blob: c855366521abc527d6ed9188ae664892aa94d8ad [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
Leo Liu78b3c832017-05-31 14:13:20 -0400168 r = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
169 AMDGPU_GEM_DOMAIN_VRAM, &adev->vce.vcpu_bo,
170 &adev->vce.gpu_addr, &adev->vce.cpu_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 if (r) {
172 dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
173 return r;
174 }
175
Christian Königc5949892016-02-10 17:43:00 +0100176 ring = &adev->vce.ring[0];
177 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
178 r = amd_sched_entity_init(&ring->sched, &adev->vce.entity,
179 rq, amdgpu_sched_jobs);
180 if (r != 0) {
181 DRM_ERROR("Failed setting up VCE run queue.\n");
182 return r;
183 }
184
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
186 atomic_set(&adev->vce.handles[i], 0);
187 adev->vce.filp[i] = NULL;
188 }
189
Christian Königebff4852016-07-20 16:53:36 +0200190 INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
191 mutex_init(&adev->vce.idle_mutex);
192
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400193 return 0;
194}
195
196/**
197 * amdgpu_vce_fini - free memory
198 *
199 * @adev: amdgpu_device pointer
200 *
201 * Last step on VCE teardown, free firmware memory
202 */
203int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
204{
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300205 unsigned i;
206
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207 if (adev->vce.vcpu_bo == NULL)
208 return 0;
209
Christian Königc5949892016-02-10 17:43:00 +0100210 amd_sched_entity_fini(&adev->vce.ring[0].sched, &adev->vce.entity);
211
Leo Liu78b3c832017-05-31 14:13:20 -0400212 amdgpu_bo_free_kernel(&adev->vce.vcpu_bo, &adev->vce.gpu_addr,
213 (void **)&adev->vce.cpu_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214
Grazvydas Ignotas4cd00d32016-09-25 23:34:49 +0300215 for (i = 0; i < adev->vce.num_rings; i++)
216 amdgpu_ring_fini(&adev->vce.ring[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400217
218 release_firmware(adev->vce.fw);
Christian Königebff4852016-07-20 16:53:36 +0200219 mutex_destroy(&adev->vce.idle_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220
221 return 0;
222}
223
224/**
225 * amdgpu_vce_suspend - unpin VCE fw memory
226 *
227 * @adev: amdgpu_device pointer
228 *
229 */
230int amdgpu_vce_suspend(struct amdgpu_device *adev)
231{
232 int i;
233
234 if (adev->vce.vcpu_bo == NULL)
235 return 0;
236
237 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
238 if (atomic_read(&adev->vce.handles[i]))
239 break;
240
241 if (i == AMDGPU_MAX_VCE_HANDLES)
242 return 0;
243
Rex Zhu85cc88f2016-04-12 19:25:52 +0800244 cancel_delayed_work_sync(&adev->vce.idle_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400245 /* TODO: suspending running encoding sessions isn't supported */
246 return -EINVAL;
247}
248
249/**
250 * amdgpu_vce_resume - pin VCE fw memory
251 *
252 * @adev: amdgpu_device pointer
253 *
254 */
255int amdgpu_vce_resume(struct amdgpu_device *adev)
256{
257 void *cpu_addr;
258 const struct common_firmware_header *hdr;
259 unsigned offset;
260 int r;
261
262 if (adev->vce.vcpu_bo == NULL)
263 return -EINVAL;
264
265 r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
266 if (r) {
267 dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
268 return r;
269 }
270
271 r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
272 if (r) {
273 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
274 dev_err(adev->dev, "(%d) VCE map failed\n", r);
275 return r;
276 }
277
278 hdr = (const struct common_firmware_header *)adev->vce.fw->data;
279 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
Christian König7b4d3e22016-08-23 11:18:59 +0200280 memcpy_toio(cpu_addr, adev->vce.fw->data + offset,
281 adev->vce.fw->size - offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282
283 amdgpu_bo_kunmap(adev->vce.vcpu_bo);
284
285 amdgpu_bo_unreserve(adev->vce.vcpu_bo);
286
287 return 0;
288}
289
290/**
291 * amdgpu_vce_idle_work_handler - power off VCE
292 *
293 * @work: pointer to work structure
294 *
295 * power of VCE when it's not used any more
296 */
297static void amdgpu_vce_idle_work_handler(struct work_struct *work)
298{
299 struct amdgpu_device *adev =
300 container_of(work, struct amdgpu_device, vce.idle_work.work);
Alex Deucher24c5fe52016-09-26 15:19:14 -0400301 unsigned i, count = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400302
Xiangliang Yud9af2252017-03-07 14:45:25 +0800303 if (amdgpu_sriov_vf(adev))
304 return;
305
Alex Deucher24c5fe52016-09-26 15:19:14 -0400306 for (i = 0; i < adev->vce.num_rings; i++)
307 count += amdgpu_fence_count_emitted(&adev->vce.ring[i]);
308
309 if (count == 0) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400310 if (adev->pm.dpm_enabled) {
311 amdgpu_dpm_enable_vce(adev, false);
312 } else {
313 amdgpu_asic_set_vce_clocks(adev, 0, 0);
Rex Zhu28ed5502017-01-25 17:35:14 +0800314 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
315 AMD_PG_STATE_GATE);
316 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
317 AMD_CG_STATE_GATE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400318 }
319 } else {
Christian König182830a2016-07-01 17:43:57 +0200320 schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400321 }
322}
323
324/**
Christian Königebff4852016-07-20 16:53:36 +0200325 * amdgpu_vce_ring_begin_use - power up VCE
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400326 *
Christian Königebff4852016-07-20 16:53:36 +0200327 * @ring: amdgpu ring
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400328 *
329 * Make sure VCE is powerd up when we want to use it
330 */
Christian Königebff4852016-07-20 16:53:36 +0200331void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332{
Christian Königebff4852016-07-20 16:53:36 +0200333 struct amdgpu_device *adev = ring->adev;
334 bool set_clocks;
Christian König182830a2016-07-01 17:43:57 +0200335
Xiangliang Yud9af2252017-03-07 14:45:25 +0800336 if (amdgpu_sriov_vf(adev))
337 return;
338
Christian Königebff4852016-07-20 16:53:36 +0200339 mutex_lock(&adev->vce.idle_mutex);
340 set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
Christian König182830a2016-07-01 17:43:57 +0200341 if (set_clocks) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342 if (adev->pm.dpm_enabled) {
343 amdgpu_dpm_enable_vce(adev, true);
344 } else {
345 amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
Rex Zhu28ed5502017-01-25 17:35:14 +0800346 amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
347 AMD_CG_STATE_UNGATE);
348 amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
349 AMD_PG_STATE_UNGATE);
350
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400351 }
352 }
Christian Königebff4852016-07-20 16:53:36 +0200353 mutex_unlock(&adev->vce.idle_mutex);
354}
355
356/**
357 * amdgpu_vce_ring_end_use - power VCE down
358 *
359 * @ring: amdgpu ring
360 *
361 * Schedule work to power VCE down again
362 */
363void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring)
364{
365 schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400366}
367
368/**
369 * amdgpu_vce_free_handles - free still open VCE handles
370 *
371 * @adev: amdgpu_device pointer
372 * @filp: drm file pointer
373 *
374 * Close all VCE handles still open by this file pointer
375 */
376void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
377{
378 struct amdgpu_ring *ring = &adev->vce.ring[0];
379 int i, r;
380 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
381 uint32_t handle = atomic_read(&adev->vce.handles[i]);
Christian König182830a2016-07-01 17:43:57 +0200382
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400383 if (!handle || adev->vce.filp[i] != filp)
384 continue;
385
Christian König9f2ade32016-02-03 16:50:56 +0100386 r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400387 if (r)
388 DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
389
390 adev->vce.filp[i] = NULL;
391 atomic_set(&adev->vce.handles[i], 0);
392 }
393}
394
395/**
396 * amdgpu_vce_get_create_msg - generate a VCE create msg
397 *
398 * @adev: amdgpu_device pointer
399 * @ring: ring we should submit the msg to
400 * @handle: VCE session handle to use
401 * @fence: optional fence to return
402 *
403 * Open up a stream for HW test
404 */
405int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100406 struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400407{
408 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100409 struct amdgpu_job *job;
410 struct amdgpu_ib *ib;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100411 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400412 uint64_t dummy;
413 int i, r;
414
Christian Königd71518b2016-02-01 12:20:25 +0100415 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
416 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417 return r;
Christian Königd71518b2016-02-01 12:20:25 +0100418
419 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400420
Chunming Zhou81287652015-07-03 14:18:26 +0800421 dummy = ib->gpu_addr + 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400422
423 /* stitch together an VCE create msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800424 ib->length_dw = 0;
425 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
426 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
427 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428
Leo Liud66f8e42015-11-18 11:57:33 -0500429 if ((ring->adev->vce.fw_version >> 24) >= 52)
430 ib->ptr[ib->length_dw++] = 0x00000040; /* len */
431 else
432 ib->ptr[ib->length_dw++] = 0x00000030; /* len */
Chunming Zhou81287652015-07-03 14:18:26 +0800433 ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
434 ib->ptr[ib->length_dw++] = 0x00000000;
435 ib->ptr[ib->length_dw++] = 0x00000042;
436 ib->ptr[ib->length_dw++] = 0x0000000a;
437 ib->ptr[ib->length_dw++] = 0x00000001;
438 ib->ptr[ib->length_dw++] = 0x00000080;
439 ib->ptr[ib->length_dw++] = 0x00000060;
440 ib->ptr[ib->length_dw++] = 0x00000100;
441 ib->ptr[ib->length_dw++] = 0x00000100;
442 ib->ptr[ib->length_dw++] = 0x0000000c;
443 ib->ptr[ib->length_dw++] = 0x00000000;
Leo Liud66f8e42015-11-18 11:57:33 -0500444 if ((ring->adev->vce.fw_version >> 24) >= 52) {
445 ib->ptr[ib->length_dw++] = 0x00000000;
446 ib->ptr[ib->length_dw++] = 0x00000000;
447 ib->ptr[ib->length_dw++] = 0x00000000;
448 ib->ptr[ib->length_dw++] = 0x00000000;
449 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450
Chunming Zhou81287652015-07-03 14:18:26 +0800451 ib->ptr[ib->length_dw++] = 0x00000014; /* len */
452 ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
453 ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
454 ib->ptr[ib->length_dw++] = dummy;
455 ib->ptr[ib->length_dw++] = 0x00000001;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400456
Chunming Zhou81287652015-07-03 14:18:26 +0800457 for (i = ib->length_dw; i < ib_size_dw; ++i)
458 ib->ptr[i] = 0x0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459
Junwei Zhang50ddc752017-01-23 16:30:38 +0800460 r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100461 job->fence = dma_fence_get(f);
Chunming Zhou81287652015-07-03 14:18:26 +0800462 if (r)
463 goto err;
Christian König9f2ade32016-02-03 16:50:56 +0100464
465 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100467 *fence = dma_fence_get(f);
468 dma_fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800469 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100470
Chunming Zhou81287652015-07-03 14:18:26 +0800471err:
Christian Königd71518b2016-02-01 12:20:25 +0100472 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473 return r;
474}
475
476/**
477 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
478 *
479 * @adev: amdgpu_device pointer
480 * @ring: ring we should submit the msg to
481 * @handle: VCE session handle to use
482 * @fence: optional fence to return
483 *
484 * Close up a stream for HW test or if userspace failed to do so
485 */
486int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100487 bool direct, struct dma_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400488{
489 const unsigned ib_size_dw = 1024;
Christian Königd71518b2016-02-01 12:20:25 +0100490 struct amdgpu_job *job;
491 struct amdgpu_ib *ib;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100492 struct dma_fence *f = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493 int i, r;
494
Christian Königd71518b2016-02-01 12:20:25 +0100495 r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
496 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400498
Christian Königd71518b2016-02-01 12:20:25 +0100499 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500
501 /* stitch together an VCE destroy msg */
Chunming Zhou81287652015-07-03 14:18:26 +0800502 ib->length_dw = 0;
503 ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
504 ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
505 ib->ptr[ib->length_dw++] = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506
Rex Zhu99453a92016-07-21 20:46:55 +0800507 ib->ptr[ib->length_dw++] = 0x00000020; /* len */
508 ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
509 ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
510 ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
511 ib->ptr[ib->length_dw++] = 0x00000000;
512 ib->ptr[ib->length_dw++] = 0x00000000;
513 ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
514 ib->ptr[ib->length_dw++] = 0x00000000;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515
Chunming Zhou81287652015-07-03 14:18:26 +0800516 ib->ptr[ib->length_dw++] = 0x00000008; /* len */
517 ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518
Chunming Zhou81287652015-07-03 14:18:26 +0800519 for (i = ib->length_dw; i < ib_size_dw; ++i)
520 ib->ptr[i] = 0x0;
Christian König9f2ade32016-02-03 16:50:56 +0100521
522 if (direct) {
Junwei Zhang50ddc752017-01-23 16:30:38 +0800523 r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100524 job->fence = dma_fence_get(f);
Christian König9f2ade32016-02-03 16:50:56 +0100525 if (r)
526 goto err;
527
528 amdgpu_job_free(job);
529 } else {
Christian Königc5949892016-02-10 17:43:00 +0100530 r = amdgpu_job_submit(job, ring, &ring->adev->vce.entity,
Christian König9f2ade32016-02-03 16:50:56 +0100531 AMDGPU_FENCE_OWNER_UNDEFINED, &f);
532 if (r)
533 goto err;
534 }
535
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100537 *fence = dma_fence_get(f);
538 dma_fence_put(f);
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800539 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100540
Chunming Zhou81287652015-07-03 14:18:26 +0800541err:
Christian Königd71518b2016-02-01 12:20:25 +0100542 amdgpu_job_free(job);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 return r;
544}
545
546/**
547 * amdgpu_vce_cs_reloc - command submission relocation
548 *
549 * @p: parser context
550 * @lo: address of lower dword
551 * @hi: address of higher dword
Christian Königf1689ec2015-06-11 20:56:18 +0200552 * @size: minimum size
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553 *
554 * Patch relocation inside command stream with real buffer address
555 */
Christian Königf1689ec2015-06-11 20:56:18 +0200556static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
Christian Königdc783302015-06-12 14:16:20 +0200557 int lo, int hi, unsigned size, uint32_t index)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400558{
559 struct amdgpu_bo_va_mapping *mapping;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 struct amdgpu_bo *bo;
561 uint64_t addr;
562
Christian Königdc783302015-06-12 14:16:20 +0200563 if (index == 0xffffffff)
564 index = 0;
565
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400566 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
567 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
Christian Königdc783302015-06-12 14:16:20 +0200568 addr += ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569
570 mapping = amdgpu_cs_find_mapping(p, addr, &bo);
571 if (mapping == NULL) {
Christian Königdc783302015-06-12 14:16:20 +0200572 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
573 addr, lo, hi, size, index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574 return -EINVAL;
575 }
576
Christian Königf1689ec2015-06-11 20:56:18 +0200577 if ((addr + (uint64_t)size) >
Christian Königa9f87f62017-03-30 14:03:59 +0200578 (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
Christian Königf1689ec2015-06-11 20:56:18 +0200579 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
580 addr, lo, hi);
581 return -EINVAL;
582 }
583
Christian Königa9f87f62017-03-30 14:03:59 +0200584 addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585 addr += amdgpu_bo_gpu_offset(bo);
Christian Königdc783302015-06-12 14:16:20 +0200586 addr -= ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587
Christian König7270f832016-01-31 11:00:41 +0100588 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
589 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400590
591 return 0;
592}
593
594/**
Christian Königf1689ec2015-06-11 20:56:18 +0200595 * amdgpu_vce_validate_handle - validate stream handle
596 *
597 * @p: parser context
598 * @handle: handle to validate
Christian König2f4b9362015-06-11 21:33:55 +0200599 * @allocated: allocated a new handle?
Christian Königf1689ec2015-06-11 20:56:18 +0200600 *
601 * Validates the handle and return the found session index or -EINVAL
602 * we we don't have another free session index.
603 */
604static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
Christian Könige5223212016-07-01 22:19:25 +0200605 uint32_t handle, uint32_t *allocated)
Christian Königf1689ec2015-06-11 20:56:18 +0200606{
607 unsigned i;
608
609 /* validate the handle */
610 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
Christian König2f4b9362015-06-11 21:33:55 +0200611 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
612 if (p->adev->vce.filp[i] != p->filp) {
613 DRM_ERROR("VCE handle collision detected!\n");
614 return -EINVAL;
615 }
Christian Königf1689ec2015-06-11 20:56:18 +0200616 return i;
Christian König2f4b9362015-06-11 21:33:55 +0200617 }
Christian Königf1689ec2015-06-11 20:56:18 +0200618 }
619
620 /* handle not found try to alloc a new one */
621 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
622 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
623 p->adev->vce.filp[i] = p->filp;
624 p->adev->vce.img_size[i] = 0;
Christian Könige5223212016-07-01 22:19:25 +0200625 *allocated |= 1 << i;
Christian Königf1689ec2015-06-11 20:56:18 +0200626 return i;
627 }
628 }
629
630 DRM_ERROR("No more free VCE handles!\n");
631 return -EINVAL;
632}
633
634/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635 * amdgpu_vce_cs_parse - parse and validate the command stream
636 *
637 * @p: parser context
638 *
639 */
640int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
641{
Christian König50838c82016-02-03 13:44:52 +0100642 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
Christian Königdc783302015-06-12 14:16:20 +0200643 unsigned fb_idx = 0, bs_idx = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200644 int session_idx = -1;
Christian Könige5223212016-07-01 22:19:25 +0200645 uint32_t destroyed = 0;
646 uint32_t created = 0;
647 uint32_t allocated = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200648 uint32_t tmp, handle = 0;
649 uint32_t *size = &tmp;
Christian Königc855e252016-09-05 17:00:57 +0200650 int i, r, idx = 0;
651
Christian König45088ef2016-10-05 16:49:19 +0200652 p->job->vm = NULL;
653 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
654
Christian Königc855e252016-09-05 17:00:57 +0200655 r = amdgpu_cs_sysvm_access_required(p);
656 if (r)
657 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400658
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 while (idx < ib->length_dw) {
660 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
661 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
662
663 if ((len < 8) || (len & 3)) {
664 DRM_ERROR("invalid VCE command length (%d)!\n", len);
Christian König2f4b9362015-06-11 21:33:55 +0200665 r = -EINVAL;
666 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 }
668
669 switch (cmd) {
Christian König182830a2016-07-01 17:43:57 +0200670 case 0x00000001: /* session */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
Christian König2f4b9362015-06-11 21:33:55 +0200672 session_idx = amdgpu_vce_validate_handle(p, handle,
673 &allocated);
Christian Könige5223212016-07-01 22:19:25 +0200674 if (session_idx < 0) {
675 r = session_idx;
676 goto out;
677 }
Christian Königf1689ec2015-06-11 20:56:18 +0200678 size = &p->adev->vce.img_size[session_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400679 break;
680
Christian König182830a2016-07-01 17:43:57 +0200681 case 0x00000002: /* task info */
Christian Königdc783302015-06-12 14:16:20 +0200682 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
683 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
Christian Königf1689ec2015-06-11 20:56:18 +0200684 break;
685
Christian König182830a2016-07-01 17:43:57 +0200686 case 0x01000001: /* create */
Christian Könige5223212016-07-01 22:19:25 +0200687 created |= 1 << session_idx;
688 if (destroyed & (1 << session_idx)) {
689 destroyed &= ~(1 << session_idx);
690 allocated |= 1 << session_idx;
691
692 } else if (!(allocated & (1 << session_idx))) {
Christian König2f4b9362015-06-11 21:33:55 +0200693 DRM_ERROR("Handle already in use!\n");
694 r = -EINVAL;
695 goto out;
696 }
697
Christian Königf1689ec2015-06-11 20:56:18 +0200698 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
699 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
700 8 * 3 / 2;
701 break;
702
Christian König182830a2016-07-01 17:43:57 +0200703 case 0x04000001: /* config extension */
704 case 0x04000002: /* pic control */
705 case 0x04000005: /* rate control */
706 case 0x04000007: /* motion estimation */
707 case 0x04000008: /* rdo */
708 case 0x04000009: /* vui */
709 case 0x05000002: /* auxiliary buffer */
Alex Deucher4f827782016-09-21 14:57:06 -0400710 case 0x05000009: /* clock table */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 break;
712
Alex Deucher5eeda8a2016-09-23 17:22:42 -0400713 case 0x0500000c: /* hw config */
714 switch (p->adev->asic_type) {
715#ifdef CONFIG_DRM_AMDGPU_CIK
716 case CHIP_KAVERI:
717 case CHIP_MULLINS:
718#endif
719 case CHIP_CARRIZO:
720 break;
721 default:
722 r = -EINVAL;
723 goto out;
724 }
725 break;
726
Christian König182830a2016-07-01 17:43:57 +0200727 case 0x03000001: /* encode */
Christian Königf1689ec2015-06-11 20:56:18 +0200728 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
Christian Königdc783302015-06-12 14:16:20 +0200729 *size, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200731 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732
Christian Königf1689ec2015-06-11 20:56:18 +0200733 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
Christian Königdc783302015-06-12 14:16:20 +0200734 *size / 3, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400735 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200736 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 break;
738
Christian König182830a2016-07-01 17:43:57 +0200739 case 0x02000001: /* destroy */
Christian Könige5223212016-07-01 22:19:25 +0200740 destroyed |= 1 << session_idx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 break;
742
Christian König182830a2016-07-01 17:43:57 +0200743 case 0x05000001: /* context buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200744 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200745 *size * 2, 0);
Christian Königf1689ec2015-06-11 20:56:18 +0200746 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200747 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200748 break;
749
Christian König182830a2016-07-01 17:43:57 +0200750 case 0x05000004: /* video bitstream buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200751 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
752 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200753 tmp, bs_idx);
Christian Königf1689ec2015-06-11 20:56:18 +0200754 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200755 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200756 break;
757
Christian König182830a2016-07-01 17:43:57 +0200758 case 0x05000005: /* feedback buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200759 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200760 4096, fb_idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200762 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763 break;
764
765 default:
766 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
Christian König2f4b9362015-06-11 21:33:55 +0200767 r = -EINVAL;
768 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400769 }
770
Christian Königf1689ec2015-06-11 20:56:18 +0200771 if (session_idx == -1) {
772 DRM_ERROR("no session command at start of IB\n");
Christian König2f4b9362015-06-11 21:33:55 +0200773 r = -EINVAL;
774 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200775 }
776
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400777 idx += len / 4;
778 }
779
Christian Könige5223212016-07-01 22:19:25 +0200780 if (allocated & ~created) {
Christian König2f4b9362015-06-11 21:33:55 +0200781 DRM_ERROR("New session without create command!\n");
782 r = -ENOENT;
783 }
784
785out:
Christian Könige5223212016-07-01 22:19:25 +0200786 if (!r) {
787 /* No error, free all destroyed handle slots */
788 tmp = destroyed;
789 } else {
790 /* Error during parsing, free all allocated handle slots */
791 tmp = allocated;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400792 }
793
Christian Könige5223212016-07-01 22:19:25 +0200794 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
795 if (tmp & (1 << i))
796 atomic_set(&p->adev->vce.handles[i], 0);
797
Christian König2f4b9362015-06-11 21:33:55 +0200798 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400799}
800
801/**
Christian König98614702016-10-10 15:23:32 +0200802 * amdgpu_vce_cs_parse_vm - parse the command stream in VM mode
803 *
804 * @p: parser context
805 *
806 */
807int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, uint32_t ib_idx)
808{
809 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
810 int session_idx = -1;
811 uint32_t destroyed = 0;
812 uint32_t created = 0;
813 uint32_t allocated = 0;
814 uint32_t tmp, handle = 0;
815 int i, r = 0, idx = 0;
816
817 while (idx < ib->length_dw) {
818 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
819 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
820
821 if ((len < 8) || (len & 3)) {
822 DRM_ERROR("invalid VCE command length (%d)!\n", len);
823 r = -EINVAL;
824 goto out;
825 }
826
827 switch (cmd) {
828 case 0x00000001: /* session */
829 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
830 session_idx = amdgpu_vce_validate_handle(p, handle,
831 &allocated);
832 if (session_idx < 0) {
833 r = session_idx;
834 goto out;
835 }
836 break;
837
838 case 0x01000001: /* create */
839 created |= 1 << session_idx;
840 if (destroyed & (1 << session_idx)) {
841 destroyed &= ~(1 << session_idx);
842 allocated |= 1 << session_idx;
843
844 } else if (!(allocated & (1 << session_idx))) {
845 DRM_ERROR("Handle already in use!\n");
846 r = -EINVAL;
847 goto out;
848 }
849
850 break;
851
852 case 0x02000001: /* destroy */
853 destroyed |= 1 << session_idx;
854 break;
855
856 default:
857 break;
858 }
859
860 if (session_idx == -1) {
861 DRM_ERROR("no session command at start of IB\n");
862 r = -EINVAL;
863 goto out;
864 }
865
866 idx += len / 4;
867 }
868
869 if (allocated & ~created) {
870 DRM_ERROR("New session without create command!\n");
871 r = -ENOENT;
872 }
873
874out:
875 if (!r) {
876 /* No error, free all destroyed handle slots */
877 tmp = destroyed;
878 amdgpu_ib_free(p->adev, ib, NULL);
879 } else {
880 /* Error during parsing, free all allocated handle slots */
881 tmp = allocated;
882 }
883
884 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
885 if (tmp & (1 << i))
886 atomic_set(&p->adev->vce.handles[i], 0);
887
888 return r;
889}
890
891/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400892 * amdgpu_vce_ring_emit_ib - execute indirect buffer
893 *
894 * @ring: engine to use
895 * @ib: the IB to execute
896 *
897 */
Christian Königd88bf582016-05-06 17:50:03 +0200898void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib,
899 unsigned vm_id, bool ctx_switch)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400900{
901 amdgpu_ring_write(ring, VCE_CMD_IB);
902 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
903 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
904 amdgpu_ring_write(ring, ib->length_dw);
905}
906
907/**
908 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
909 *
910 * @ring: engine to use
911 * @fence: the fence
912 *
913 */
914void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +0800915 unsigned flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400916{
Chunming Zhou890ee232015-06-01 14:35:03 +0800917 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400918
919 amdgpu_ring_write(ring, VCE_CMD_FENCE);
920 amdgpu_ring_write(ring, addr);
921 amdgpu_ring_write(ring, upper_32_bits(addr));
922 amdgpu_ring_write(ring, seq);
923 amdgpu_ring_write(ring, VCE_CMD_TRAP);
924 amdgpu_ring_write(ring, VCE_CMD_END);
925}
926
927/**
928 * amdgpu_vce_ring_test_ring - test if VCE ring is working
929 *
930 * @ring: the engine to test on
931 *
932 */
933int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
934{
935 struct amdgpu_device *adev = ring->adev;
936 uint32_t rptr = amdgpu_ring_get_rptr(ring);
937 unsigned i;
Xiangliang Yua2f537e2017-04-06 14:43:48 +0800938 int r, timeout = adev->usec_timeout;
939
Frank Mina1b90222017-06-12 11:02:09 +0800940 /* skip ring test for sriov*/
Xiangliang Yua2f537e2017-04-06 14:43:48 +0800941 if (amdgpu_sriov_vf(adev))
Frank Mina1b90222017-06-12 11:02:09 +0800942 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943
Christian Königa27de352016-01-21 11:28:53 +0100944 r = amdgpu_ring_alloc(ring, 16);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400945 if (r) {
946 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
947 ring->idx, r);
948 return r;
949 }
950 amdgpu_ring_write(ring, VCE_CMD_END);
Christian Königa27de352016-01-21 11:28:53 +0100951 amdgpu_ring_commit(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952
Xiangliang Yua2f537e2017-04-06 14:43:48 +0800953 for (i = 0; i < timeout; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954 if (amdgpu_ring_get_rptr(ring) != rptr)
955 break;
956 DRM_UDELAY(1);
957 }
958
Xiangliang Yua2f537e2017-04-06 14:43:48 +0800959 if (i < timeout) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960 DRM_INFO("ring test on %d succeeded in %d usecs\n",
961 ring->idx, i);
962 } else {
963 DRM_ERROR("amdgpu: ring %d test failed\n",
964 ring->idx);
965 r = -ETIMEDOUT;
966 }
967
968 return r;
969}
970
971/**
972 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
973 *
974 * @ring: the engine to test on
975 *
976 */
Christian Königbbec97a2016-07-05 21:07:17 +0200977int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100979 struct dma_fence *fence = NULL;
Christian Königbbec97a2016-07-05 21:07:17 +0200980 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981
Alex Deucher6f0359f2016-08-24 17:15:33 -0400982 /* skip vce ring1/2 ib test for now, since it's not reliable */
983 if (ring != &ring->adev->vce.ring[0])
Leo Liu898e50d2015-09-04 15:08:55 -0400984 return 0;
985
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
987 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200988 DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989 goto error;
990 }
991
Christian König9f2ade32016-02-03 16:50:56 +0100992 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400993 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200994 DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 goto error;
996 }
997
Chris Wilsonf54d1862016-10-25 13:00:45 +0100998 r = dma_fence_wait_timeout(fence, false, timeout);
Christian Königbbec97a2016-07-05 21:07:17 +0200999 if (r == 0) {
1000 DRM_ERROR("amdgpu: IB test timed out.\n");
1001 r = -ETIMEDOUT;
1002 } else if (r < 0) {
1003 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001004 } else {
1005 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
Christian Königbbec97a2016-07-05 21:07:17 +02001006 r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007 }
1008error:
Chris Wilsonf54d1862016-10-25 13:00:45 +01001009 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 return r;
1011}