blob: e2c06780ce49e6a934480ee853ab6ff92bbd4a91 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2013 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 * Authors: Christian König <christian.koenig@amd.com>
26 */
27
28#include <linux/firmware.h>
29#include <linux/module.h>
30#include <drm/drmP.h>
31#include <drm/drm.h>
32
33#include "amdgpu.h"
34#include "amdgpu_pm.h"
35#include "amdgpu_vce.h"
36#include "cikd.h"
37
38/* 1 second timeout */
Christian König182830a2016-07-01 17:43:57 +020039#define VCE_IDLE_TIMEOUT msecs_to_jiffies(1000)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040
41/* Firmware Names */
42#ifdef CONFIG_DRM_AMDGPU_CIK
43#define FIRMWARE_BONAIRE "radeon/bonaire_vce.bin"
Christian Königedf600d2016-05-03 15:54:54 +020044#define FIRMWARE_KABINI "radeon/kabini_vce.bin"
45#define FIRMWARE_KAVERI "radeon/kaveri_vce.bin"
46#define FIRMWARE_HAWAII "radeon/hawaii_vce.bin"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040047#define FIRMWARE_MULLINS "radeon/mullins_vce.bin"
48#endif
Jammy Zhouc65444f2015-05-13 22:49:04 +080049#define FIRMWARE_TONGA "amdgpu/tonga_vce.bin"
50#define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin"
Alex Deucher188a9bc2015-07-27 14:24:14 -040051#define FIRMWARE_FIJI "amdgpu/fiji_vce.bin"
Samuel Licfaba562015-10-08 16:27:55 -040052#define FIRMWARE_STONEY "amdgpu/stoney_vce.bin"
Flora Cui2cc0c0b2016-03-14 18:33:29 -040053#define FIRMWARE_POLARIS10 "amdgpu/polaris10_vce.bin"
54#define FIRMWARE_POLARIS11 "amdgpu/polaris11_vce.bin"
Junwei Zhangc4642a42016-12-14 15:32:28 -050055#define FIRMWARE_POLARIS12 "amdgpu/polaris12_vce.bin"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040056
57#ifdef CONFIG_DRM_AMDGPU_CIK
58MODULE_FIRMWARE(FIRMWARE_BONAIRE);
59MODULE_FIRMWARE(FIRMWARE_KABINI);
60MODULE_FIRMWARE(FIRMWARE_KAVERI);
61MODULE_FIRMWARE(FIRMWARE_HAWAII);
62MODULE_FIRMWARE(FIRMWARE_MULLINS);
63#endif
64MODULE_FIRMWARE(FIRMWARE_TONGA);
65MODULE_FIRMWARE(FIRMWARE_CARRIZO);
Alex Deucher188a9bc2015-07-27 14:24:14 -040066MODULE_FIRMWARE(FIRMWARE_FIJI);
Samuel Licfaba562015-10-08 16:27:55 -040067MODULE_FIRMWARE(FIRMWARE_STONEY);
Flora Cui2cc0c0b2016-03-14 18:33:29 -040068MODULE_FIRMWARE(FIRMWARE_POLARIS10);
69MODULE_FIRMWARE(FIRMWARE_POLARIS11);
Junwei Zhangc4642a42016-12-14 15:32:28 -050070MODULE_FIRMWARE(FIRMWARE_POLARIS12);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040071
72static void amdgpu_vce_idle_work_handler(struct work_struct *work);
73
74/**
75 * amdgpu_vce_init - allocate memory, load vce firmware
76 *
77 * @adev: amdgpu_device pointer
78 *
79 * First step to get VCE online, allocate memory and load the firmware
80 */
Leo Liue9822622015-05-06 14:31:27 -040081int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082{
Christian Königc5949892016-02-10 17:43:00 +010083 struct amdgpu_ring *ring;
84 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 const char *fw_name;
86 const struct common_firmware_header *hdr;
87 unsigned ucode_version, version_major, version_minor, binary_id;
88 int i, r;
89
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 switch (adev->asic_type) {
91#ifdef CONFIG_DRM_AMDGPU_CIK
92 case CHIP_BONAIRE:
93 fw_name = FIRMWARE_BONAIRE;
94 break;
95 case CHIP_KAVERI:
96 fw_name = FIRMWARE_KAVERI;
97 break;
98 case CHIP_KABINI:
99 fw_name = FIRMWARE_KABINI;
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_CARRIZO:
112 fw_name = FIRMWARE_CARRIZO;
113 break;
Alex Deucher188a9bc2015-07-27 14:24:14 -0400114 case CHIP_FIJI:
115 fw_name = FIRMWARE_FIJI;
116 break;
Samuel Licfaba562015-10-08 16:27:55 -0400117 case CHIP_STONEY:
118 fw_name = FIRMWARE_STONEY;
119 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400120 case CHIP_POLARIS10:
121 fw_name = FIRMWARE_POLARIS10;
Sonny Jiang1b4eeea2016-03-11 14:33:40 -0500122 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400123 case CHIP_POLARIS11:
124 fw_name = FIRMWARE_POLARIS11;
Sonny Jiang1b4eeea2016-03-11 14:33:40 -0500125 break;
Junwei Zhangc4642a42016-12-14 15:32:28 -0500126 case CHIP_POLARIS12:
127 fw_name = FIRMWARE_POLARIS12;
128 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129
130 default:
131 return -EINVAL;
132 }
133
134 r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
135 if (r) {
136 dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
137 fw_name);
138 return r;
139 }
140
141 r = amdgpu_ucode_validate(adev->vce.fw);
142 if (r) {
143 dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
144 fw_name);
145 release_firmware(adev->vce.fw);
146 adev->vce.fw = NULL;
147 return r;
148 }
149
150 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
151
152 ucode_version = le32_to_cpu(hdr->ucode_version);
153 version_major = (ucode_version >> 20) & 0xfff;
154 version_minor = (ucode_version >> 8) & 0xfff;
155 binary_id = ucode_version & 0xff;
156 DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
157 version_major, version_minor, binary_id);
158 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
159 (binary_id << 8));
160
161 /* allocate firmware, stack and heap BO */
162
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163 r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -0400164 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200165 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
166 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200167 NULL, NULL, &adev->vce.vcpu_bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 if (r) {
169 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
170 return r;
171 }
172
173 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
174 if (r) {
175 amdgpu_bo_unref(&adev->vce.vcpu_bo);
176 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
177 return r;
178 }
179
180 r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
181 &adev->vce.gpu_addr);
182 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
183 if (r) {
184 amdgpu_bo_unref(&adev->vce.vcpu_bo);
185 dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
186 return r;
187 }
188
Christian Königc5949892016-02-10 17:43:00 +0100189
190 ring = &adev->vce.ring[0];
191 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
192 r = amd_sched_entity_init(&ring->sched, &adev->vce.entity,
193 rq, amdgpu_sched_jobs);
194 if (r != 0) {
195 DRM_ERROR("Failed setting up VCE run queue.\n");
196 return r;
197 }
198
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
200 atomic_set(&adev->vce.handles[i], 0);
201 adev->vce.filp[i] = NULL;
202 }
203
Christian Königebff4852016-07-20 16:53:36 +0200204 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
205 mutex_init(&adev->vce.idle_mutex);
206
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207 return 0;
208}
209
210/**
211 * amdgpu_vce_fini - free memory
212 *
213 * @adev: amdgpu_device pointer
214 *
215 * Last step on VCE teardown, free firmware memory
216 */
217int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
218{
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300219 unsigned i;
220
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400221 if (adev->vce.vcpu_bo == NULL)
222 return 0;
223
Christian Königc5949892016-02-10 17:43:00 +0100224 amd_sched_entity_fini(&adev->vce.ring[0].sched, &adev->vce.entity);
225
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226 amdgpu_bo_unref(&adev->vce.vcpu_bo);
227
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300228 for (i = 0; i < adev->vce.num_rings; i++)
229 amdgpu_ring_fini(&adev->vce.ring[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400230
231 release_firmware(adev->vce.fw);
Christian Königebff4852016-07-20 16:53:36 +0200232 mutex_destroy(&adev->vce.idle_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233
234 return 0;
235}
236
237/**
238 * amdgpu_vce_suspend - unpin VCE fw memory
239 *
240 * @adev: amdgpu_device pointer
241 *
242 */
243int amdgpu_vce_suspend(struct amdgpu_device *adev)
244{
245 int i;
246
247 if (adev->vce.vcpu_bo == NULL)
248 return 0;
249
250 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
251 if (atomic_read(&adev->vce.handles[i]))
252 break;
253
254 if (i == AMDGPU_MAX_VCE_HANDLES)
255 return 0;
256
Rex Zhu85cc88f2016-04-12 19:25:52 +0800257 cancel_delayed_work_sync(&adev->vce.idle_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400258 /* TODO: suspending running encoding sessions isn't supported */
259 return -EINVAL;
260}
261
262/**
263 * amdgpu_vce_resume - pin VCE fw memory
264 *
265 * @adev: amdgpu_device pointer
266 *
267 */
268int amdgpu_vce_resume(struct amdgpu_device *adev)
269{
270 void *cpu_addr;
271 const struct common_firmware_header *hdr;
272 unsigned offset;
273 int r;
274
275 if (adev->vce.vcpu_bo == NULL)
276 return -EINVAL;
277
278 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
279 if (r) {
280 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
281 return r;
282 }
283
284 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
285 if (r) {
286 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
287 dev_err(adev->dev, "(%d) VCE map failed\n", r);
288 return r;
289 }
290
291 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
292 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
Christian König7b4d3e22016-08-23 11:18:59 +0200293 memcpy_toio(cpu_addr, adev->vce.fw->data + offset,
294 adev->vce.fw->size - offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400295
296 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
297
298 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
299
300 return 0;
301}
302
303/**
304 * amdgpu_vce_idle_work_handler - power off VCE
305 *
306 * @work: pointer to work structure
307 *
308 * power of VCE when it's not used any more
309 */
310static void amdgpu_vce_idle_work_handler(struct work_struct *work)
311{
312 struct amdgpu_device *adev =
313 container_of(work, struct amdgpu_device, vce.idle_work.work);
Alex Deucher24c5fe52016-09-26 15:19:14 -0400314 unsigned i, count = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315
Alex Deucher24c5fe52016-09-26 15:19:14 -0400316 for (i = 0; i < adev->vce.num_rings; i++)
317 count += amdgpu_fence_count_emitted(&adev->vce.ring[i]);
318
319 if (count == 0) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400320 if (adev->pm.dpm_enabled) {
321 amdgpu_dpm_enable_vce(adev, false);
322 } else {
323 amdgpu_asic_set_vce_clocks(adev, 0, 0);
Rex Zhu28ed5502017-01-25 17:35:14 +0800324 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
325 AMD_PG_STATE_GATE);
326 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
327 AMD_CG_STATE_GATE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400328 }
329 } else {
Christian König182830a2016-07-01 17:43:57 +0200330 schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331 }
332}
333
334/**
Christian Königebff4852016-07-20 16:53:36 +0200335 * amdgpu_vce_ring_begin_use - power up VCE
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400336 *
Christian Königebff4852016-07-20 16:53:36 +0200337 * @ring: amdgpu ring
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400338 *
339 * Make sure VCE is powerd up when we want to use it
340 */
Christian Königebff4852016-07-20 16:53:36 +0200341void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342{
Christian Königebff4852016-07-20 16:53:36 +0200343 struct amdgpu_device *adev = ring->adev;
344 bool set_clocks;
Christian König182830a2016-07-01 17:43:57 +0200345
Christian Königebff4852016-07-20 16:53:36 +0200346 mutex_lock(&adev->vce.idle_mutex);
347 set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
Christian König182830a2016-07-01 17:43:57 +0200348 if (set_clocks) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400349 if (adev->pm.dpm_enabled) {
350 amdgpu_dpm_enable_vce(adev, true);
351 } else {
352 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
Rex Zhu28ed5502017-01-25 17:35:14 +0800353 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
354 AMD_CG_STATE_UNGATE);
355 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
356 AMD_PG_STATE_UNGATE);
357
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400358 }
359 }
Christian Königebff4852016-07-20 16:53:36 +0200360 mutex_unlock(&adev->vce.idle_mutex);
361}
362
363/**
364 * amdgpu_vce_ring_end_use - power VCE down
365 *
366 * @ring: amdgpu ring
367 *
368 * Schedule work to power VCE down again
369 */
370void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring)
371{
372 schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400373}
374
375/**
376 * amdgpu_vce_free_handles - free still open VCE handles
377 *
378 * @adev: amdgpu_device pointer
379 * @filp: drm file pointer
380 *
381 * Close all VCE handles still open by this file pointer
382 */
383void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
384{
385 struct amdgpu_ring *ring = &adev->vce.ring[0];
386 int i, r;
387 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
388 uint32_t handle = atomic_read(&adev->vce.handles[i]);
Christian König182830a2016-07-01 17:43:57 +0200389
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400390 if (!handle || adev->vce.filp[i] != filp)
391 continue;
392
Christian König9f2ade32016-02-03 16:50:56 +0100393 r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400394 if (r)
395 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
396
397 adev->vce.filp[i] = NULL;
398 atomic_set(&adev->vce.handles[i], 0);
399 }
400}
401
402/**
403 * amdgpu_vce_get_create_msg - generate a VCE create msg
404 *
405 * @adev: amdgpu_device pointer
406 * @ring: ring we should submit the msg to
407 * @handle: VCE session handle to use
408 * @fence: optional fence to return
409 *
410 * Open up a stream for HW test
411 */
412int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100413 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400414{
415 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100416 struct amdgpu_job *job;
417 struct amdgpu_ib *ib;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100418 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419 uint64_t dummy;
420 int i, r;
421
Christian Königd71518b2016-02-01 12:20:25 +0100422 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
423 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100425
426 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400427
Chunming Zhou81287652015-07-03 14:18:26 +0800428 dummy = ib->gpu_addr + 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429
430 /* stitch together an VCE create msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800431 ib->length_dw = 0;
432 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
433 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
434 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400435
Leo Liud66f8e42015-11-18 11:57:33 -0500436 if ((ring->adev->vce.fw_version >> 24) >= 52)
437 ib->ptr[ib->length_dw++] = 0x00000040; /* len */
438 else
439 ib->ptr[ib->length_dw++] = 0x00000030; /* len */
Chunming Zhou81287652015-07-03 14:18:26 +0800440 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
441 ib->ptr[ib->length_dw++] = 0x00000000;
442 ib->ptr[ib->length_dw++] = 0x00000042;
443 ib->ptr[ib->length_dw++] = 0x0000000a;
444 ib->ptr[ib->length_dw++] = 0x00000001;
445 ib->ptr[ib->length_dw++] = 0x00000080;
446 ib->ptr[ib->length_dw++] = 0x00000060;
447 ib->ptr[ib->length_dw++] = 0x00000100;
448 ib->ptr[ib->length_dw++] = 0x00000100;
449 ib->ptr[ib->length_dw++] = 0x0000000c;
450 ib->ptr[ib->length_dw++] = 0x00000000;
Leo Liud66f8e42015-11-18 11:57:33 -0500451 if ((ring->adev->vce.fw_version >> 24) >= 52) {
452 ib->ptr[ib->length_dw++] = 0x00000000;
453 ib->ptr[ib->length_dw++] = 0x00000000;
454 ib->ptr[ib->length_dw++] = 0x00000000;
455 ib->ptr[ib->length_dw++] = 0x00000000;
456 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457
Chunming Zhou81287652015-07-03 14:18:26 +0800458 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
459 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
460 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
461 ib->ptr[ib->length_dw++] = dummy;
462 ib->ptr[ib->length_dw++] = 0x00000001;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463
Chunming Zhou81287652015-07-03 14:18:26 +0800464 for (i = ib->length_dw; i < ib_size_dw; ++i)
465 ib->ptr[i] = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466
Junwei Zhang50ddc752017-01-23 16:30:38 +0800467 r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100468 job->fence = dma_fence_get(f);
Chunming Zhou81287652015-07-03 14:18:26 +0800469 if (r)
470 goto err;
Christian König9f2ade32016-02-03 16:50:56 +0100471
472 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100474 *fence = dma_fence_get(f);
475 dma_fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800476 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100477
Chunming Zhou81287652015-07-03 14:18:26 +0800478err:
Christian Königd71518b2016-02-01 12:20:25 +0100479 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480 return r;
481}
482
483/**
484 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
485 *
486 * @adev: amdgpu_device pointer
487 * @ring: ring we should submit the msg to
488 * @handle: VCE session handle to use
489 * @fence: optional fence to return
490 *
491 * Close up a stream for HW test or if userspace failed to do so
492 */
493int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100494 bool direct, struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400495{
496 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100497 struct amdgpu_job *job;
498 struct amdgpu_ib *ib;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100499 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 int i, r;
501
Christian Königd71518b2016-02-01 12:20:25 +0100502 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
503 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505
Christian Königd71518b2016-02-01 12:20:25 +0100506 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507
508 /* stitch together an VCE destroy msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800509 ib->length_dw = 0;
510 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
511 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
512 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513
Rex Zhu99453a92016-07-21 20:46:55 +0800514 ib->ptr[ib->length_dw++] = 0x00000020; /* len */
515 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
516 ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
517 ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
518 ib->ptr[ib->length_dw++] = 0x00000000;
519 ib->ptr[ib->length_dw++] = 0x00000000;
520 ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
521 ib->ptr[ib->length_dw++] = 0x00000000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522
Chunming Zhou81287652015-07-03 14:18:26 +0800523 ib->ptr[ib->length_dw++] = 0x00000008; /* len */
524 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525
Chunming Zhou81287652015-07-03 14:18:26 +0800526 for (i = ib->length_dw; i < ib_size_dw; ++i)
527 ib->ptr[i] = 0x0;
Christian König9f2ade32016-02-03 16:50:56 +0100528
529 if (direct) {
Junwei Zhang50ddc752017-01-23 16:30:38 +0800530 r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100531 job->fence = dma_fence_get(f);
Christian König9f2ade32016-02-03 16:50:56 +0100532 if (r)
533 goto err;
534
535 amdgpu_job_free(job);
536 } else {
Christian Königc5949892016-02-10 17:43:00 +0100537 r = amdgpu_job_submit(job, ring, &ring->adev->vce.entity,
Christian König9f2ade32016-02-03 16:50:56 +0100538 AMDGPU_FENCE_OWNER_UNDEFINED, &f);
539 if (r)
540 goto err;
541 }
542
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100544 *fence = dma_fence_get(f);
545 dma_fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800546 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100547
Chunming Zhou81287652015-07-03 14:18:26 +0800548err:
Christian Königd71518b2016-02-01 12:20:25 +0100549 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550 return r;
551}
552
553/**
554 * amdgpu_vce_cs_reloc - command submission relocation
555 *
556 * @p: parser context
557 * @lo: address of lower dword
558 * @hi: address of higher dword
Christian Königf1689ec2015-06-11 20:56:18 +0200559 * @size: minimum size
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 *
561 * Patch relocation inside command stream with real buffer address
562 */
Christian Königf1689ec2015-06-11 20:56:18 +0200563static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
Christian Königdc783302015-06-12 14:16:20 +0200564 int lo, int hi, unsigned size, uint32_t index)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400565{
566 struct amdgpu_bo_va_mapping *mapping;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400567 struct amdgpu_bo *bo;
568 uint64_t addr;
569
Christian Königdc783302015-06-12 14:16:20 +0200570 if (index == 0xffffffff)
571 index = 0;
572
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
574 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
Christian Königdc783302015-06-12 14:16:20 +0200575 addr += ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576
577 mapping = amdgpu_cs_find_mapping(p, addr, &bo);
578 if (mapping == NULL) {
Christian Königdc783302015-06-12 14:16:20 +0200579 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
580 addr, lo, hi, size, index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 return -EINVAL;
582 }
583
Christian Königf1689ec2015-06-11 20:56:18 +0200584 if ((addr + (uint64_t)size) >
585 ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
586 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
587 addr, lo, hi);
588 return -EINVAL;
589 }
590
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
592 addr += amdgpu_bo_gpu_offset(bo);
Christian Königdc783302015-06-12 14:16:20 +0200593 addr -= ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594
Christian König7270f832016-01-31 11:00:41 +0100595 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
596 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597
598 return 0;
599}
600
601/**
Christian Königf1689ec2015-06-11 20:56:18 +0200602 * amdgpu_vce_validate_handle - validate stream handle
603 *
604 * @p: parser context
605 * @handle: handle to validate
Christian König2f4b9362015-06-11 21:33:55 +0200606 * @allocated: allocated a new handle?
Christian Königf1689ec2015-06-11 20:56:18 +0200607 *
608 * Validates the handle and return the found session index or -EINVAL
609 * we we don't have another free session index.
610 */
611static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
Christian Könige5223212016-07-01 22:19:25 +0200612 uint32_t handle, uint32_t *allocated)
Christian Königf1689ec2015-06-11 20:56:18 +0200613{
614 unsigned i;
615
616 /* validate the handle */
617 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
Christian König2f4b9362015-06-11 21:33:55 +0200618 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
619 if (p->adev->vce.filp[i] != p->filp) {
620 DRM_ERROR("VCE handle collision detected!\n");
621 return -EINVAL;
622 }
Christian Königf1689ec2015-06-11 20:56:18 +0200623 return i;
Christian König2f4b9362015-06-11 21:33:55 +0200624 }
Christian Königf1689ec2015-06-11 20:56:18 +0200625 }
626
627 /* handle not found try to alloc a new one */
628 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
629 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
630 p->adev->vce.filp[i] = p->filp;
631 p->adev->vce.img_size[i] = 0;
Christian Könige5223212016-07-01 22:19:25 +0200632 *allocated |= 1 << i;
Christian Königf1689ec2015-06-11 20:56:18 +0200633 return i;
634 }
635 }
636
637 DRM_ERROR("No more free VCE handles!\n");
638 return -EINVAL;
639}
640
641/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 * amdgpu_vce_cs_parse - parse and validate the command stream
643 *
644 * @p: parser context
645 *
646 */
647int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
648{
Christian König50838c82016-02-03 13:44:52 +0100649 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
Christian Königdc783302015-06-12 14:16:20 +0200650 unsigned fb_idx = 0, bs_idx = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200651 int session_idx = -1;
Christian Könige5223212016-07-01 22:19:25 +0200652 uint32_t destroyed = 0;
653 uint32_t created = 0;
654 uint32_t allocated = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200655 uint32_t tmp, handle = 0;
656 uint32_t *size = &tmp;
Christian Königc855e252016-09-05 17:00:57 +0200657 int i, r, idx = 0;
658
Christian König45088ef2016-10-05 16:49:19 +0200659 p->job->vm = NULL;
660 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
661
Christian Königc855e252016-09-05 17:00:57 +0200662 r = amdgpu_cs_sysvm_access_required(p);
663 if (r)
664 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666 while (idx < ib->length_dw) {
667 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
668 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
669
670 if ((len < 8) || (len & 3)) {
671 DRM_ERROR("invalid VCE command length (%d)!\n", len);
Christian König2f4b9362015-06-11 21:33:55 +0200672 r = -EINVAL;
673 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400674 }
675
676 switch (cmd) {
Christian König182830a2016-07-01 17:43:57 +0200677 case 0x00000001: /* session */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
Christian König2f4b9362015-06-11 21:33:55 +0200679 session_idx = amdgpu_vce_validate_handle(p, handle,
680 &allocated);
Christian Könige5223212016-07-01 22:19:25 +0200681 if (session_idx < 0) {
682 r = session_idx;
683 goto out;
684 }
Christian Königf1689ec2015-06-11 20:56:18 +0200685 size = &p->adev->vce.img_size[session_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 break;
687
Christian König182830a2016-07-01 17:43:57 +0200688 case 0x00000002: /* task info */
Christian Königdc783302015-06-12 14:16:20 +0200689 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
690 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
Christian Königf1689ec2015-06-11 20:56:18 +0200691 break;
692
Christian König182830a2016-07-01 17:43:57 +0200693 case 0x01000001: /* create */
Christian Könige5223212016-07-01 22:19:25 +0200694 created |= 1 << session_idx;
695 if (destroyed & (1 << session_idx)) {
696 destroyed &= ~(1 << session_idx);
697 allocated |= 1 << session_idx;
698
699 } else if (!(allocated & (1 << session_idx))) {
Christian König2f4b9362015-06-11 21:33:55 +0200700 DRM_ERROR("Handle already in use!\n");
701 r = -EINVAL;
702 goto out;
703 }
704
Christian Königf1689ec2015-06-11 20:56:18 +0200705 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
706 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
707 8 * 3 / 2;
708 break;
709
Christian König182830a2016-07-01 17:43:57 +0200710 case 0x04000001: /* config extension */
711 case 0x04000002: /* pic control */
712 case 0x04000005: /* rate control */
713 case 0x04000007: /* motion estimation */
714 case 0x04000008: /* rdo */
715 case 0x04000009: /* vui */
716 case 0x05000002: /* auxiliary buffer */
Alex Deucher4f827782016-09-21 14:57:06 -0400717 case 0x05000009: /* clock table */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400718 break;
719
Alex Deucher5eeda8a2016-09-23 17:22:42 -0400720 case 0x0500000c: /* hw config */
721 switch (p->adev->asic_type) {
722#ifdef CONFIG_DRM_AMDGPU_CIK
723 case CHIP_KAVERI:
724 case CHIP_MULLINS:
725#endif
726 case CHIP_CARRIZO:
727 break;
728 default:
729 r = -EINVAL;
730 goto out;
731 }
732 break;
733
Christian König182830a2016-07-01 17:43:57 +0200734 case 0x03000001: /* encode */
Christian Königf1689ec2015-06-11 20:56:18 +0200735 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
Christian Königdc783302015-06-12 14:16:20 +0200736 *size, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200738 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739
Christian Königf1689ec2015-06-11 20:56:18 +0200740 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
Christian Königdc783302015-06-12 14:16:20 +0200741 *size / 3, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400742 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200743 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744 break;
745
Christian König182830a2016-07-01 17:43:57 +0200746 case 0x02000001: /* destroy */
Christian Könige5223212016-07-01 22:19:25 +0200747 destroyed |= 1 << session_idx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748 break;
749
Christian König182830a2016-07-01 17:43:57 +0200750 case 0x05000001: /* context buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200751 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200752 *size * 2, 0);
Christian Königf1689ec2015-06-11 20:56:18 +0200753 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200754 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200755 break;
756
Christian König182830a2016-07-01 17:43:57 +0200757 case 0x05000004: /* video bitstream buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200758 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
759 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200760 tmp, bs_idx);
Christian Königf1689ec2015-06-11 20:56:18 +0200761 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200762 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200763 break;
764
Christian König182830a2016-07-01 17:43:57 +0200765 case 0x05000005: /* feedback buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200766 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200767 4096, fb_idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400768 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200769 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770 break;
771
772 default:
773 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
Christian König2f4b9362015-06-11 21:33:55 +0200774 r = -EINVAL;
775 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776 }
777
Christian Königf1689ec2015-06-11 20:56:18 +0200778 if (session_idx == -1) {
779 DRM_ERROR("no session command at start of IB\n");
Christian König2f4b9362015-06-11 21:33:55 +0200780 r = -EINVAL;
781 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200782 }
783
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784 idx += len / 4;
785 }
786
Christian Könige5223212016-07-01 22:19:25 +0200787 if (allocated & ~created) {
Christian König2f4b9362015-06-11 21:33:55 +0200788 DRM_ERROR("New session without create command!\n");
789 r = -ENOENT;
790 }
791
792out:
Christian Könige5223212016-07-01 22:19:25 +0200793 if (!r) {
794 /* No error, free all destroyed handle slots */
795 tmp = destroyed;
796 } else {
797 /* Error during parsing, free all allocated handle slots */
798 tmp = allocated;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400799 }
800
Christian Könige5223212016-07-01 22:19:25 +0200801 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
802 if (tmp & (1 << i))
803 atomic_set(&p->adev->vce.handles[i], 0);
804
Christian König2f4b9362015-06-11 21:33:55 +0200805 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400806}
807
808/**
Christian König98614702016-10-10 15:23:32 +0200809 * amdgpu_vce_cs_parse_vm - parse the command stream in VM mode
810 *
811 * @p: parser context
812 *
813 */
814int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, uint32_t ib_idx)
815{
816 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
817 int session_idx = -1;
818 uint32_t destroyed = 0;
819 uint32_t created = 0;
820 uint32_t allocated = 0;
821 uint32_t tmp, handle = 0;
822 int i, r = 0, idx = 0;
823
824 while (idx < ib->length_dw) {
825 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
826 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
827
828 if ((len < 8) || (len & 3)) {
829 DRM_ERROR("invalid VCE command length (%d)!\n", len);
830 r = -EINVAL;
831 goto out;
832 }
833
834 switch (cmd) {
835 case 0x00000001: /* session */
836 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
837 session_idx = amdgpu_vce_validate_handle(p, handle,
838 &allocated);
839 if (session_idx < 0) {
840 r = session_idx;
841 goto out;
842 }
843 break;
844
845 case 0x01000001: /* create */
846 created |= 1 << session_idx;
847 if (destroyed & (1 << session_idx)) {
848 destroyed &= ~(1 << session_idx);
849 allocated |= 1 << session_idx;
850
851 } else if (!(allocated & (1 << session_idx))) {
852 DRM_ERROR("Handle already in use!\n");
853 r = -EINVAL;
854 goto out;
855 }
856
857 break;
858
859 case 0x02000001: /* destroy */
860 destroyed |= 1 << session_idx;
861 break;
862
863 default:
864 break;
865 }
866
867 if (session_idx == -1) {
868 DRM_ERROR("no session command at start of IB\n");
869 r = -EINVAL;
870 goto out;
871 }
872
873 idx += len / 4;
874 }
875
876 if (allocated & ~created) {
877 DRM_ERROR("New session without create command!\n");
878 r = -ENOENT;
879 }
880
881out:
882 if (!r) {
883 /* No error, free all destroyed handle slots */
884 tmp = destroyed;
885 amdgpu_ib_free(p->adev, ib, NULL);
886 } else {
887 /* Error during parsing, free all allocated handle slots */
888 tmp = allocated;
889 }
890
891 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
892 if (tmp & (1 << i))
893 atomic_set(&p->adev->vce.handles[i], 0);
894
895 return r;
896}
897
898/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899 * amdgpu_vce_ring_emit_ib - execute indirect buffer
900 *
901 * @ring: engine to use
902 * @ib: the IB to execute
903 *
904 */
Christian Königd88bf582016-05-06 17:50:03 +0200905void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib,
906 unsigned vm_id, bool ctx_switch)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400907{
908 amdgpu_ring_write(ring, VCE_CMD_IB);
909 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
910 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
911 amdgpu_ring_write(ring, ib->length_dw);
912}
913
914/**
915 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
916 *
917 * @ring: engine to use
918 * @fence: the fence
919 *
920 */
921void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +0800922 unsigned flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400923{
Chunming Zhou890ee232015-06-01 14:35:03 +0800924 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925
926 amdgpu_ring_write(ring, VCE_CMD_FENCE);
927 amdgpu_ring_write(ring, addr);
928 amdgpu_ring_write(ring, upper_32_bits(addr));
929 amdgpu_ring_write(ring, seq);
930 amdgpu_ring_write(ring, VCE_CMD_TRAP);
931 amdgpu_ring_write(ring, VCE_CMD_END);
932}
933
934/**
935 * amdgpu_vce_ring_test_ring - test if VCE ring is working
936 *
937 * @ring: the engine to test on
938 *
939 */
940int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
941{
942 struct amdgpu_device *adev = ring->adev;
943 uint32_t rptr = amdgpu_ring_get_rptr(ring);
944 unsigned i;
945 int r;
946
Christian Königa27de352016-01-21 11:28:53 +0100947 r = amdgpu_ring_alloc(ring, 16);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948 if (r) {
949 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
950 ring->idx, r);
951 return r;
952 }
953 amdgpu_ring_write(ring, VCE_CMD_END);
Christian Königa27de352016-01-21 11:28:53 +0100954 amdgpu_ring_commit(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955
956 for (i = 0; i < adev->usec_timeout; i++) {
957 if (amdgpu_ring_get_rptr(ring) != rptr)
958 break;
959 DRM_UDELAY(1);
960 }
961
962 if (i < adev->usec_timeout) {
963 DRM_INFO("ring test on %d succeeded in %d usecs\n",
964 ring->idx, i);
965 } else {
966 DRM_ERROR("amdgpu: ring %d test failed\n",
967 ring->idx);
968 r = -ETIMEDOUT;
969 }
970
971 return r;
972}
973
974/**
975 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
976 *
977 * @ring: the engine to test on
978 *
979 */
Christian Königbbec97a2016-07-05 21:07:17 +0200980int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100982 struct dma_fence *fence = NULL;
Christian Königbbec97a2016-07-05 21:07:17 +0200983 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984
Alex Deucher6f0359f2016-08-24 17:15:33 -0400985 /* skip vce ring1/2 ib test for now, since it's not reliable */
986 if (ring != &ring->adev->vce.ring[0])
Leo Liu898e50d2015-09-04 15:08:55 -0400987 return 0;
988
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
990 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200991 DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400992 goto error;
993 }
994
Christian König9f2ade32016-02-03 16:50:56 +0100995 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200997 DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400998 goto error;
999 }
1000
Chris Wilsonf54d1862016-10-25 13:00:45 +01001001 r = dma_fence_wait_timeout(fence, false, timeout);
Christian Königbbec97a2016-07-05 21:07:17 +02001002 if (r == 0) {
1003 DRM_ERROR("amdgpu: IB test timed out.\n");
1004 r = -ETIMEDOUT;
1005 } else if (r < 0) {
1006 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007 } else {
1008 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
Christian Königbbec97a2016-07-05 21:07:17 +02001009 r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 }
1011error:
Chris Wilsonf54d1862016-10-25 13:00:45 +01001012 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001013 return r;
1014}