blob: 735c38d7db0d7da44eff5239d448095d677be6b6 [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
Leo Liuc1dc3562017-03-03 18:27:49 -050057#define FIRMWARE_VEGA10 "amdgpu/vega10_vce.bin"
58
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059#ifdef CONFIG_DRM_AMDGPU_CIK
60MODULE_FIRMWARE(FIRMWARE_BONAIRE);
61MODULE_FIRMWARE(FIRMWARE_KABINI);
62MODULE_FIRMWARE(FIRMWARE_KAVERI);
63MODULE_FIRMWARE(FIRMWARE_HAWAII);
64MODULE_FIRMWARE(FIRMWARE_MULLINS);
65#endif
66MODULE_FIRMWARE(FIRMWARE_TONGA);
67MODULE_FIRMWARE(FIRMWARE_CARRIZO);
Alex Deucher188a9bc2015-07-27 14:24:14 -040068MODULE_FIRMWARE(FIRMWARE_FIJI);
Samuel Licfaba562015-10-08 16:27:55 -040069MODULE_FIRMWARE(FIRMWARE_STONEY);
Flora Cui2cc0c0b2016-03-14 18:33:29 -040070MODULE_FIRMWARE(FIRMWARE_POLARIS10);
71MODULE_FIRMWARE(FIRMWARE_POLARIS11);
Junwei Zhangc4642a42016-12-14 15:32:28 -050072MODULE_FIRMWARE(FIRMWARE_POLARIS12);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073
Leo Liuc1dc3562017-03-03 18:27:49 -050074MODULE_FIRMWARE(FIRMWARE_VEGA10);
75
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076static void amdgpu_vce_idle_work_handler(struct work_struct *work);
77
78/**
79 * amdgpu_vce_init - allocate memory, load vce firmware
80 *
81 * @adev: amdgpu_device pointer
82 *
83 * First step to get VCE online, allocate memory and load the firmware
84 */
Leo Liue9822622015-05-06 14:31:27 -040085int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086{
Christian Königc5949892016-02-10 17:43:00 +010087 struct amdgpu_ring *ring;
88 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 const char *fw_name;
90 const struct common_firmware_header *hdr;
91 unsigned ucode_version, version_major, version_minor, binary_id;
92 int i, r;
93
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 switch (adev->asic_type) {
95#ifdef CONFIG_DRM_AMDGPU_CIK
96 case CHIP_BONAIRE:
97 fw_name = FIRMWARE_BONAIRE;
98 break;
99 case CHIP_KAVERI:
100 fw_name = FIRMWARE_KAVERI;
101 break;
102 case CHIP_KABINI:
103 fw_name = FIRMWARE_KABINI;
104 break;
105 case CHIP_HAWAII:
106 fw_name = FIRMWARE_HAWAII;
107 break;
108 case CHIP_MULLINS:
109 fw_name = FIRMWARE_MULLINS;
110 break;
111#endif
112 case CHIP_TONGA:
113 fw_name = FIRMWARE_TONGA;
114 break;
115 case CHIP_CARRIZO:
116 fw_name = FIRMWARE_CARRIZO;
117 break;
Alex Deucher188a9bc2015-07-27 14:24:14 -0400118 case CHIP_FIJI:
119 fw_name = FIRMWARE_FIJI;
120 break;
Samuel Licfaba562015-10-08 16:27:55 -0400121 case CHIP_STONEY:
122 fw_name = FIRMWARE_STONEY;
123 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400124 case CHIP_POLARIS10:
125 fw_name = FIRMWARE_POLARIS10;
Sonny Jiang1b4eeea2016-03-11 14:33:40 -0500126 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400127 case CHIP_POLARIS11:
128 fw_name = FIRMWARE_POLARIS11;
Sonny Jiang1b4eeea2016-03-11 14:33:40 -0500129 break;
Leo Liuc1dc3562017-03-03 18:27:49 -0500130 case CHIP_VEGA10:
131 fw_name = FIRMWARE_VEGA10;
132 break;
Junwei Zhangc4642a42016-12-14 15:32:28 -0500133 case CHIP_POLARIS12:
134 fw_name = FIRMWARE_POLARIS12;
135 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136
137 default:
138 return -EINVAL;
139 }
140
141 r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
142 if (r) {
143 dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
144 fw_name);
145 return r;
146 }
147
148 r = amdgpu_ucode_validate(adev->vce.fw);
149 if (r) {
150 dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
151 fw_name);
152 release_firmware(adev->vce.fw);
153 adev->vce.fw = NULL;
154 return r;
155 }
156
157 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
158
159 ucode_version = le32_to_cpu(hdr->ucode_version);
160 version_major = (ucode_version >> 20) & 0xfff;
161 version_minor = (ucode_version >> 8) & 0xfff;
162 binary_id = ucode_version & 0xff;
163 DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
164 version_major, version_minor, binary_id);
165 adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
166 (binary_id << 8));
167
168 /* allocate firmware, stack and heap BO */
169
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170 r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -0400171 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200172 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
173 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200174 NULL, NULL, &adev->vce.vcpu_bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175 if (r) {
176 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
177 return r;
178 }
179
180 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
181 if (r) {
182 amdgpu_bo_unref(&adev->vce.vcpu_bo);
183 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
184 return r;
185 }
186
187 r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
188 &adev->vce.gpu_addr);
189 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
190 if (r) {
191 amdgpu_bo_unref(&adev->vce.vcpu_bo);
192 dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
193 return r;
194 }
195
Christian Königc5949892016-02-10 17:43:00 +0100196
197 ring = &adev->vce.ring[0];
198 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
199 r = amd_sched_entity_init(&ring->sched, &adev->vce.entity,
200 rq, amdgpu_sched_jobs);
201 if (r != 0) {
202 DRM_ERROR("Failed setting up VCE run queue.\n");
203 return r;
204 }
205
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
207 atomic_set(&adev->vce.handles[i], 0);
208 adev->vce.filp[i] = NULL;
209 }
210
Christian Königebff4852016-07-20 16:53:36 +0200211 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
212 mutex_init(&adev->vce.idle_mutex);
213
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214 return 0;
215}
216
217/**
218 * amdgpu_vce_fini - free memory
219 *
220 * @adev: amdgpu_device pointer
221 *
222 * Last step on VCE teardown, free firmware memory
223 */
224int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
225{
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300226 unsigned i;
227
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228 if (adev->vce.vcpu_bo == NULL)
229 return 0;
230
Christian Königc5949892016-02-10 17:43:00 +0100231 amd_sched_entity_fini(&adev->vce.ring[0].sched, &adev->vce.entity);
232
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233 amdgpu_bo_unref(&adev->vce.vcpu_bo);
234
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300235 for (i = 0; i < adev->vce.num_rings; i++)
236 amdgpu_ring_fini(&adev->vce.ring[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400237
238 release_firmware(adev->vce.fw);
Christian Königebff4852016-07-20 16:53:36 +0200239 mutex_destroy(&adev->vce.idle_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240
241 return 0;
242}
243
244/**
245 * amdgpu_vce_suspend - unpin VCE fw memory
246 *
247 * @adev: amdgpu_device pointer
248 *
249 */
250int amdgpu_vce_suspend(struct amdgpu_device *adev)
251{
252 int i;
253
254 if (adev->vce.vcpu_bo == NULL)
255 return 0;
256
257 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
258 if (atomic_read(&adev->vce.handles[i]))
259 break;
260
261 if (i == AMDGPU_MAX_VCE_HANDLES)
262 return 0;
263
Rex Zhu85cc88f2016-04-12 19:25:52 +0800264 cancel_delayed_work_sync(&adev->vce.idle_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265 /* TODO: suspending running encoding sessions isn't supported */
266 return -EINVAL;
267}
268
269/**
270 * amdgpu_vce_resume - pin VCE fw memory
271 *
272 * @adev: amdgpu_device pointer
273 *
274 */
275int amdgpu_vce_resume(struct amdgpu_device *adev)
276{
277 void *cpu_addr;
278 const struct common_firmware_header *hdr;
279 unsigned offset;
280 int r;
281
282 if (adev->vce.vcpu_bo == NULL)
283 return -EINVAL;
284
285 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
286 if (r) {
287 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
288 return r;
289 }
290
291 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
292 if (r) {
293 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
294 dev_err(adev->dev, "(%d) VCE map failed\n", r);
295 return r;
296 }
297
298 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
299 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
Christian König7b4d3e22016-08-23 11:18:59 +0200300 memcpy_toio(cpu_addr, adev->vce.fw->data + offset,
301 adev->vce.fw->size - offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400302
303 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
304
305 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
306
307 return 0;
308}
309
310/**
311 * amdgpu_vce_idle_work_handler - power off VCE
312 *
313 * @work: pointer to work structure
314 *
315 * power of VCE when it's not used any more
316 */
317static void amdgpu_vce_idle_work_handler(struct work_struct *work)
318{
319 struct amdgpu_device *adev =
320 container_of(work, struct amdgpu_device, vce.idle_work.work);
Alex Deucher24c5fe52016-09-26 15:19:14 -0400321 unsigned i, count = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400322
Xiangliang Yud9af2252017-03-07 14:45:25 +0800323 if (amdgpu_sriov_vf(adev))
324 return;
325
Alex Deucher24c5fe52016-09-26 15:19:14 -0400326 for (i = 0; i < adev->vce.num_rings; i++)
327 count += amdgpu_fence_count_emitted(&adev->vce.ring[i]);
328
329 if (count == 0) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400330 if (adev->pm.dpm_enabled) {
331 amdgpu_dpm_enable_vce(adev, false);
332 } else {
333 amdgpu_asic_set_vce_clocks(adev, 0, 0);
Rex Zhu28ed5502017-01-25 17:35:14 +0800334 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
335 AMD_PG_STATE_GATE);
336 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
337 AMD_CG_STATE_GATE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400338 }
339 } else {
Christian König182830a2016-07-01 17:43:57 +0200340 schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400341 }
342}
343
344/**
Christian Königebff4852016-07-20 16:53:36 +0200345 * amdgpu_vce_ring_begin_use - power up VCE
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 *
Christian Königebff4852016-07-20 16:53:36 +0200347 * @ring: amdgpu ring
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 *
349 * Make sure VCE is powerd up when we want to use it
350 */
Christian Königebff4852016-07-20 16:53:36 +0200351void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400352{
Christian Königebff4852016-07-20 16:53:36 +0200353 struct amdgpu_device *adev = ring->adev;
354 bool set_clocks;
Christian König182830a2016-07-01 17:43:57 +0200355
Xiangliang Yud9af2252017-03-07 14:45:25 +0800356 if (amdgpu_sriov_vf(adev))
357 return;
358
Christian Königebff4852016-07-20 16:53:36 +0200359 mutex_lock(&adev->vce.idle_mutex);
360 set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
Christian König182830a2016-07-01 17:43:57 +0200361 if (set_clocks) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400362 if (adev->pm.dpm_enabled) {
363 amdgpu_dpm_enable_vce(adev, true);
364 } else {
365 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
Rex Zhu28ed5502017-01-25 17:35:14 +0800366 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
367 AMD_CG_STATE_UNGATE);
368 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
369 AMD_PG_STATE_UNGATE);
370
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400371 }
372 }
Christian Königebff4852016-07-20 16:53:36 +0200373 mutex_unlock(&adev->vce.idle_mutex);
374}
375
376/**
377 * amdgpu_vce_ring_end_use - power VCE down
378 *
379 * @ring: amdgpu ring
380 *
381 * Schedule work to power VCE down again
382 */
383void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring)
384{
385 schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400386}
387
388/**
389 * amdgpu_vce_free_handles - free still open VCE handles
390 *
391 * @adev: amdgpu_device pointer
392 * @filp: drm file pointer
393 *
394 * Close all VCE handles still open by this file pointer
395 */
396void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
397{
398 struct amdgpu_ring *ring = &adev->vce.ring[0];
399 int i, r;
400 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
401 uint32_t handle = atomic_read(&adev->vce.handles[i]);
Christian König182830a2016-07-01 17:43:57 +0200402
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400403 if (!handle || adev->vce.filp[i] != filp)
404 continue;
405
Christian König9f2ade32016-02-03 16:50:56 +0100406 r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400407 if (r)
408 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
409
410 adev->vce.filp[i] = NULL;
411 atomic_set(&adev->vce.handles[i], 0);
412 }
413}
414
415/**
416 * amdgpu_vce_get_create_msg - generate a VCE create msg
417 *
418 * @adev: amdgpu_device pointer
419 * @ring: ring we should submit the msg to
420 * @handle: VCE session handle to use
421 * @fence: optional fence to return
422 *
423 * Open up a stream for HW test
424 */
425int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100426 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400427{
428 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100429 struct amdgpu_job *job;
430 struct amdgpu_ib *ib;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100431 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432 uint64_t dummy;
433 int i, r;
434
Christian Königd71518b2016-02-01 12:20:25 +0100435 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
436 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100438
439 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400440
Chunming Zhou81287652015-07-03 14:18:26 +0800441 dummy = ib->gpu_addr + 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400442
443 /* stitch together an VCE create msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800444 ib->length_dw = 0;
445 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
446 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
447 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448
Leo Liud66f8e42015-11-18 11:57:33 -0500449 if ((ring->adev->vce.fw_version >> 24) >= 52)
450 ib->ptr[ib->length_dw++] = 0x00000040; /* len */
451 else
452 ib->ptr[ib->length_dw++] = 0x00000030; /* len */
Chunming Zhou81287652015-07-03 14:18:26 +0800453 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
454 ib->ptr[ib->length_dw++] = 0x00000000;
455 ib->ptr[ib->length_dw++] = 0x00000042;
456 ib->ptr[ib->length_dw++] = 0x0000000a;
457 ib->ptr[ib->length_dw++] = 0x00000001;
458 ib->ptr[ib->length_dw++] = 0x00000080;
459 ib->ptr[ib->length_dw++] = 0x00000060;
460 ib->ptr[ib->length_dw++] = 0x00000100;
461 ib->ptr[ib->length_dw++] = 0x00000100;
462 ib->ptr[ib->length_dw++] = 0x0000000c;
463 ib->ptr[ib->length_dw++] = 0x00000000;
Leo Liud66f8e42015-11-18 11:57:33 -0500464 if ((ring->adev->vce.fw_version >> 24) >= 52) {
465 ib->ptr[ib->length_dw++] = 0x00000000;
466 ib->ptr[ib->length_dw++] = 0x00000000;
467 ib->ptr[ib->length_dw++] = 0x00000000;
468 ib->ptr[ib->length_dw++] = 0x00000000;
469 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470
Chunming Zhou81287652015-07-03 14:18:26 +0800471 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
472 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
473 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
474 ib->ptr[ib->length_dw++] = dummy;
475 ib->ptr[ib->length_dw++] = 0x00000001;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400476
Chunming Zhou81287652015-07-03 14:18:26 +0800477 for (i = ib->length_dw; i < ib_size_dw; ++i)
478 ib->ptr[i] = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479
Junwei Zhang50ddc752017-01-23 16:30:38 +0800480 r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100481 job->fence = dma_fence_get(f);
Chunming Zhou81287652015-07-03 14:18:26 +0800482 if (r)
483 goto err;
Christian König9f2ade32016-02-03 16:50:56 +0100484
485 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100487 *fence = dma_fence_get(f);
488 dma_fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800489 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100490
Chunming Zhou81287652015-07-03 14:18:26 +0800491err:
Christian Königd71518b2016-02-01 12:20:25 +0100492 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493 return r;
494}
495
496/**
497 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
498 *
499 * @adev: amdgpu_device pointer
500 * @ring: ring we should submit the msg to
501 * @handle: VCE session handle to use
502 * @fence: optional fence to return
503 *
504 * Close up a stream for HW test or if userspace failed to do so
505 */
506int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100507 bool direct, struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508{
509 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100510 struct amdgpu_job *job;
511 struct amdgpu_ib *ib;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100512 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 int i, r;
514
Christian Königd71518b2016-02-01 12:20:25 +0100515 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
516 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518
Christian Königd71518b2016-02-01 12:20:25 +0100519 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520
521 /* stitch together an VCE destroy msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800522 ib->length_dw = 0;
523 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
524 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
525 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526
Rex Zhu99453a92016-07-21 20:46:55 +0800527 ib->ptr[ib->length_dw++] = 0x00000020; /* len */
528 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
529 ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
530 ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
531 ib->ptr[ib->length_dw++] = 0x00000000;
532 ib->ptr[ib->length_dw++] = 0x00000000;
533 ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
534 ib->ptr[ib->length_dw++] = 0x00000000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535
Chunming Zhou81287652015-07-03 14:18:26 +0800536 ib->ptr[ib->length_dw++] = 0x00000008; /* len */
537 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400538
Chunming Zhou81287652015-07-03 14:18:26 +0800539 for (i = ib->length_dw; i < ib_size_dw; ++i)
540 ib->ptr[i] = 0x0;
Christian König9f2ade32016-02-03 16:50:56 +0100541
542 if (direct) {
Junwei Zhang50ddc752017-01-23 16:30:38 +0800543 r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100544 job->fence = dma_fence_get(f);
Christian König9f2ade32016-02-03 16:50:56 +0100545 if (r)
546 goto err;
547
548 amdgpu_job_free(job);
549 } else {
Christian Königc5949892016-02-10 17:43:00 +0100550 r = amdgpu_job_submit(job, ring, &ring->adev->vce.entity,
Christian König9f2ade32016-02-03 16:50:56 +0100551 AMDGPU_FENCE_OWNER_UNDEFINED, &f);
552 if (r)
553 goto err;
554 }
555
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100557 *fence = dma_fence_get(f);
558 dma_fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800559 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100560
Chunming Zhou81287652015-07-03 14:18:26 +0800561err:
Christian Königd71518b2016-02-01 12:20:25 +0100562 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 return r;
564}
565
566/**
567 * amdgpu_vce_cs_reloc - command submission relocation
568 *
569 * @p: parser context
570 * @lo: address of lower dword
571 * @hi: address of higher dword
Christian Königf1689ec2015-06-11 20:56:18 +0200572 * @size: minimum size
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 *
574 * Patch relocation inside command stream with real buffer address
575 */
Christian Königf1689ec2015-06-11 20:56:18 +0200576static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
Christian Königdc783302015-06-12 14:16:20 +0200577 int lo, int hi, unsigned size, uint32_t index)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578{
579 struct amdgpu_bo_va_mapping *mapping;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 struct amdgpu_bo *bo;
581 uint64_t addr;
582
Christian Königdc783302015-06-12 14:16:20 +0200583 if (index == 0xffffffff)
584 index = 0;
585
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
587 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
Christian Königdc783302015-06-12 14:16:20 +0200588 addr += ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589
590 mapping = amdgpu_cs_find_mapping(p, addr, &bo);
591 if (mapping == NULL) {
Christian Königdc783302015-06-12 14:16:20 +0200592 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
593 addr, lo, hi, size, index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 return -EINVAL;
595 }
596
Christian Königf1689ec2015-06-11 20:56:18 +0200597 if ((addr + (uint64_t)size) >
Christian Königa9f87f62017-03-30 14:03:59 +0200598 (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
Christian Königf1689ec2015-06-11 20:56:18 +0200599 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
600 addr, lo, hi);
601 return -EINVAL;
602 }
603
Christian Königa9f87f62017-03-30 14:03:59 +0200604 addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400605 addr += amdgpu_bo_gpu_offset(bo);
Christian Königdc783302015-06-12 14:16:20 +0200606 addr -= ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400607
Christian König7270f832016-01-31 11:00:41 +0100608 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
609 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610
611 return 0;
612}
613
614/**
Christian Königf1689ec2015-06-11 20:56:18 +0200615 * amdgpu_vce_validate_handle - validate stream handle
616 *
617 * @p: parser context
618 * @handle: handle to validate
Christian König2f4b9362015-06-11 21:33:55 +0200619 * @allocated: allocated a new handle?
Christian Königf1689ec2015-06-11 20:56:18 +0200620 *
621 * Validates the handle and return the found session index or -EINVAL
622 * we we don't have another free session index.
623 */
624static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
Christian Könige5223212016-07-01 22:19:25 +0200625 uint32_t handle, uint32_t *allocated)
Christian Königf1689ec2015-06-11 20:56:18 +0200626{
627 unsigned i;
628
629 /* validate the handle */
630 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
Christian König2f4b9362015-06-11 21:33:55 +0200631 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
632 if (p->adev->vce.filp[i] != p->filp) {
633 DRM_ERROR("VCE handle collision detected!\n");
634 return -EINVAL;
635 }
Christian Königf1689ec2015-06-11 20:56:18 +0200636 return i;
Christian König2f4b9362015-06-11 21:33:55 +0200637 }
Christian Königf1689ec2015-06-11 20:56:18 +0200638 }
639
640 /* handle not found try to alloc a new one */
641 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
642 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
643 p->adev->vce.filp[i] = p->filp;
644 p->adev->vce.img_size[i] = 0;
Christian Könige5223212016-07-01 22:19:25 +0200645 *allocated |= 1 << i;
Christian Königf1689ec2015-06-11 20:56:18 +0200646 return i;
647 }
648 }
649
650 DRM_ERROR("No more free VCE handles!\n");
651 return -EINVAL;
652}
653
654/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655 * amdgpu_vce_cs_parse - parse and validate the command stream
656 *
657 * @p: parser context
658 *
659 */
660int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
661{
Christian König50838c82016-02-03 13:44:52 +0100662 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
Christian Königdc783302015-06-12 14:16:20 +0200663 unsigned fb_idx = 0, bs_idx = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200664 int session_idx = -1;
Christian Könige5223212016-07-01 22:19:25 +0200665 uint32_t destroyed = 0;
666 uint32_t created = 0;
667 uint32_t allocated = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200668 uint32_t tmp, handle = 0;
669 uint32_t *size = &tmp;
Christian Königc855e252016-09-05 17:00:57 +0200670 int i, r, idx = 0;
671
Christian König45088ef2016-10-05 16:49:19 +0200672 p->job->vm = NULL;
673 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
674
Christian Königc855e252016-09-05 17:00:57 +0200675 r = amdgpu_cs_sysvm_access_required(p);
676 if (r)
677 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400679 while (idx < ib->length_dw) {
680 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
681 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
682
683 if ((len < 8) || (len & 3)) {
684 DRM_ERROR("invalid VCE command length (%d)!\n", len);
Christian König2f4b9362015-06-11 21:33:55 +0200685 r = -EINVAL;
686 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687 }
688
689 switch (cmd) {
Christian König182830a2016-07-01 17:43:57 +0200690 case 0x00000001: /* session */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400691 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
Christian König2f4b9362015-06-11 21:33:55 +0200692 session_idx = amdgpu_vce_validate_handle(p, handle,
693 &allocated);
Christian Könige5223212016-07-01 22:19:25 +0200694 if (session_idx < 0) {
695 r = session_idx;
696 goto out;
697 }
Christian Königf1689ec2015-06-11 20:56:18 +0200698 size = &p->adev->vce.img_size[session_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 break;
700
Christian König182830a2016-07-01 17:43:57 +0200701 case 0x00000002: /* task info */
Christian Königdc783302015-06-12 14:16:20 +0200702 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
703 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
Christian Königf1689ec2015-06-11 20:56:18 +0200704 break;
705
Christian König182830a2016-07-01 17:43:57 +0200706 case 0x01000001: /* create */
Christian Könige5223212016-07-01 22:19:25 +0200707 created |= 1 << session_idx;
708 if (destroyed & (1 << session_idx)) {
709 destroyed &= ~(1 << session_idx);
710 allocated |= 1 << session_idx;
711
712 } else if (!(allocated & (1 << session_idx))) {
Christian König2f4b9362015-06-11 21:33:55 +0200713 DRM_ERROR("Handle already in use!\n");
714 r = -EINVAL;
715 goto out;
716 }
717
Christian Königf1689ec2015-06-11 20:56:18 +0200718 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
719 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
720 8 * 3 / 2;
721 break;
722
Christian König182830a2016-07-01 17:43:57 +0200723 case 0x04000001: /* config extension */
724 case 0x04000002: /* pic control */
725 case 0x04000005: /* rate control */
726 case 0x04000007: /* motion estimation */
727 case 0x04000008: /* rdo */
728 case 0x04000009: /* vui */
729 case 0x05000002: /* auxiliary buffer */
Alex Deucher4f827782016-09-21 14:57:06 -0400730 case 0x05000009: /* clock table */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731 break;
732
Alex Deucher5eeda8a2016-09-23 17:22:42 -0400733 case 0x0500000c: /* hw config */
734 switch (p->adev->asic_type) {
735#ifdef CONFIG_DRM_AMDGPU_CIK
736 case CHIP_KAVERI:
737 case CHIP_MULLINS:
738#endif
739 case CHIP_CARRIZO:
740 break;
741 default:
742 r = -EINVAL;
743 goto out;
744 }
745 break;
746
Christian König182830a2016-07-01 17:43:57 +0200747 case 0x03000001: /* encode */
Christian Königf1689ec2015-06-11 20:56:18 +0200748 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
Christian Königdc783302015-06-12 14:16:20 +0200749 *size, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400750 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200751 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752
Christian Königf1689ec2015-06-11 20:56:18 +0200753 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
Christian Königdc783302015-06-12 14:16:20 +0200754 *size / 3, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400755 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200756 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757 break;
758
Christian König182830a2016-07-01 17:43:57 +0200759 case 0x02000001: /* destroy */
Christian Könige5223212016-07-01 22:19:25 +0200760 destroyed |= 1 << session_idx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 break;
762
Christian König182830a2016-07-01 17:43:57 +0200763 case 0x05000001: /* context buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200764 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200765 *size * 2, 0);
Christian Königf1689ec2015-06-11 20:56:18 +0200766 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200767 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200768 break;
769
Christian König182830a2016-07-01 17:43:57 +0200770 case 0x05000004: /* video bitstream buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200771 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
772 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200773 tmp, bs_idx);
Christian Königf1689ec2015-06-11 20:56:18 +0200774 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200775 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200776 break;
777
Christian König182830a2016-07-01 17:43:57 +0200778 case 0x05000005: /* feedback buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200779 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200780 4096, fb_idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400781 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200782 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400783 break;
784
785 default:
786 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
Christian König2f4b9362015-06-11 21:33:55 +0200787 r = -EINVAL;
788 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 }
790
Christian Königf1689ec2015-06-11 20:56:18 +0200791 if (session_idx == -1) {
792 DRM_ERROR("no session command at start of IB\n");
Christian König2f4b9362015-06-11 21:33:55 +0200793 r = -EINVAL;
794 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200795 }
796
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797 idx += len / 4;
798 }
799
Christian Könige5223212016-07-01 22:19:25 +0200800 if (allocated & ~created) {
Christian König2f4b9362015-06-11 21:33:55 +0200801 DRM_ERROR("New session without create command!\n");
802 r = -ENOENT;
803 }
804
805out:
Christian Könige5223212016-07-01 22:19:25 +0200806 if (!r) {
807 /* No error, free all destroyed handle slots */
808 tmp = destroyed;
809 } else {
810 /* Error during parsing, free all allocated handle slots */
811 tmp = allocated;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400812 }
813
Christian Könige5223212016-07-01 22:19:25 +0200814 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
815 if (tmp & (1 << i))
816 atomic_set(&p->adev->vce.handles[i], 0);
817
Christian König2f4b9362015-06-11 21:33:55 +0200818 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819}
820
821/**
Christian König98614702016-10-10 15:23:32 +0200822 * amdgpu_vce_cs_parse_vm - parse the command stream in VM mode
823 *
824 * @p: parser context
825 *
826 */
827int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, uint32_t ib_idx)
828{
829 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
830 int session_idx = -1;
831 uint32_t destroyed = 0;
832 uint32_t created = 0;
833 uint32_t allocated = 0;
834 uint32_t tmp, handle = 0;
835 int i, r = 0, idx = 0;
836
837 while (idx < ib->length_dw) {
838 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
839 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
840
841 if ((len < 8) || (len & 3)) {
842 DRM_ERROR("invalid VCE command length (%d)!\n", len);
843 r = -EINVAL;
844 goto out;
845 }
846
847 switch (cmd) {
848 case 0x00000001: /* session */
849 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
850 session_idx = amdgpu_vce_validate_handle(p, handle,
851 &allocated);
852 if (session_idx < 0) {
853 r = session_idx;
854 goto out;
855 }
856 break;
857
858 case 0x01000001: /* create */
859 created |= 1 << session_idx;
860 if (destroyed & (1 << session_idx)) {
861 destroyed &= ~(1 << session_idx);
862 allocated |= 1 << session_idx;
863
864 } else if (!(allocated & (1 << session_idx))) {
865 DRM_ERROR("Handle already in use!\n");
866 r = -EINVAL;
867 goto out;
868 }
869
870 break;
871
872 case 0x02000001: /* destroy */
873 destroyed |= 1 << session_idx;
874 break;
875
876 default:
877 break;
878 }
879
880 if (session_idx == -1) {
881 DRM_ERROR("no session command at start of IB\n");
882 r = -EINVAL;
883 goto out;
884 }
885
886 idx += len / 4;
887 }
888
889 if (allocated & ~created) {
890 DRM_ERROR("New session without create command!\n");
891 r = -ENOENT;
892 }
893
894out:
895 if (!r) {
896 /* No error, free all destroyed handle slots */
897 tmp = destroyed;
898 amdgpu_ib_free(p->adev, ib, NULL);
899 } else {
900 /* Error during parsing, free all allocated handle slots */
901 tmp = allocated;
902 }
903
904 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
905 if (tmp & (1 << i))
906 atomic_set(&p->adev->vce.handles[i], 0);
907
908 return r;
909}
910
911/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400912 * amdgpu_vce_ring_emit_ib - execute indirect buffer
913 *
914 * @ring: engine to use
915 * @ib: the IB to execute
916 *
917 */
Christian Königd88bf582016-05-06 17:50:03 +0200918void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib,
919 unsigned vm_id, bool ctx_switch)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400920{
921 amdgpu_ring_write(ring, VCE_CMD_IB);
922 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
923 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
924 amdgpu_ring_write(ring, ib->length_dw);
925}
926
927/**
928 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
929 *
930 * @ring: engine to use
931 * @fence: the fence
932 *
933 */
934void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +0800935 unsigned flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936{
Chunming Zhou890ee232015-06-01 14:35:03 +0800937 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938
939 amdgpu_ring_write(ring, VCE_CMD_FENCE);
940 amdgpu_ring_write(ring, addr);
941 amdgpu_ring_write(ring, upper_32_bits(addr));
942 amdgpu_ring_write(ring, seq);
943 amdgpu_ring_write(ring, VCE_CMD_TRAP);
944 amdgpu_ring_write(ring, VCE_CMD_END);
945}
946
947/**
948 * amdgpu_vce_ring_test_ring - test if VCE ring is working
949 *
950 * @ring: the engine to test on
951 *
952 */
953int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
954{
955 struct amdgpu_device *adev = ring->adev;
956 uint32_t rptr = amdgpu_ring_get_rptr(ring);
957 unsigned i;
Xiangliang Yua2f537e2017-04-06 14:43:48 +0800958 int r, timeout = adev->usec_timeout;
959
960 /* workaround VCE ring test slow issue for sriov*/
961 if (amdgpu_sriov_vf(adev))
962 timeout *= 10;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963
Christian Königa27de352016-01-21 11:28:53 +0100964 r = amdgpu_ring_alloc(ring, 16);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 if (r) {
966 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
967 ring->idx, r);
968 return r;
969 }
970 amdgpu_ring_write(ring, VCE_CMD_END);
Christian Königa27de352016-01-21 11:28:53 +0100971 amdgpu_ring_commit(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972
Xiangliang Yua2f537e2017-04-06 14:43:48 +0800973 for (i = 0; i < timeout; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 if (amdgpu_ring_get_rptr(ring) != rptr)
975 break;
976 DRM_UDELAY(1);
977 }
978
Xiangliang Yua2f537e2017-04-06 14:43:48 +0800979 if (i < timeout) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980 DRM_INFO("ring test on %d succeeded in %d usecs\n",
981 ring->idx, i);
982 } else {
983 DRM_ERROR("amdgpu: ring %d test failed\n",
984 ring->idx);
985 r = -ETIMEDOUT;
986 }
987
988 return r;
989}
990
991/**
992 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
993 *
994 * @ring: the engine to test on
995 *
996 */
Christian Königbbec97a2016-07-05 21:07:17 +0200997int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400998{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100999 struct dma_fence *fence = NULL;
Christian Königbbec97a2016-07-05 21:07:17 +02001000 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001
Alex Deucher6f0359f2016-08-24 17:15:33 -04001002 /* skip vce ring1/2 ib test for now, since it's not reliable */
1003 if (ring != &ring->adev->vce.ring[0])
Leo Liu898e50d2015-09-04 15:08:55 -04001004 return 0;
1005
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
1007 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +02001008 DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009 goto error;
1010 }
1011
Christian König9f2ade32016-02-03 16:50:56 +01001012 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001013 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +02001014 DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001015 goto error;
1016 }
1017
Chris Wilsonf54d1862016-10-25 13:00:45 +01001018 r = dma_fence_wait_timeout(fence, false, timeout);
Christian Königbbec97a2016-07-05 21:07:17 +02001019 if (r == 0) {
1020 DRM_ERROR("amdgpu: IB test timed out.\n");
1021 r = -ETIMEDOUT;
1022 } else if (r < 0) {
1023 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024 } else {
1025 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
Christian Königbbec97a2016-07-05 21:07:17 +02001026 r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001027 }
1028error:
Chris Wilsonf54d1862016-10-25 13:00:45 +01001029 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001030 return r;
1031}