blob: a91abfb327469270225a2cbe510bfe33fc95f158 [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,
Monk Liub3eebe32017-10-23 12:23:29 +0800179 rq, amdgpu_sched_jobs, NULL);
Christian Königc5949892016-02-10 17:43:00 +0100180 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/**
Christian König23594312017-11-17 11:09:43 +0100547 * amdgpu_vce_cs_validate_bo - make sure not to cross 4GB boundary
548 *
549 * @p: parser context
550 * @lo: address of lower dword
551 * @hi: address of higher dword
552 * @size: minimum size
553 * @index: bs/fb index
554 *
555 * Make sure that no BO cross a 4GB boundary.
556 */
557static int amdgpu_vce_validate_bo(struct amdgpu_cs_parser *p, uint32_t ib_idx,
558 int lo, int hi, unsigned size, int32_t index)
559{
560 int64_t offset = ((uint64_t)size) * ((int64_t)index);
561 struct amdgpu_bo_va_mapping *mapping;
562 unsigned i, fpfn, lpfn;
563 struct amdgpu_bo *bo;
564 uint64_t addr;
565 int r;
566
567 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
568 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
569 if (index >= 0) {
570 addr += offset;
571 fpfn = PAGE_ALIGN(offset) >> PAGE_SHIFT;
572 lpfn = 0x100000000ULL >> PAGE_SHIFT;
573 } else {
574 fpfn = 0;
575 lpfn = (0x100000000ULL - PAGE_ALIGN(offset)) >> PAGE_SHIFT;
576 }
577
578 r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping);
579 if (r) {
580 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
581 addr, lo, hi, size, index);
582 return r;
583 }
584
585 for (i = 0; i < bo->placement.num_placement; ++i) {
586 bo->placements[i].fpfn = max(bo->placements[i].fpfn, fpfn);
587 bo->placements[i].lpfn = bo->placements[i].fpfn ?
588 min(bo->placements[i].fpfn, lpfn) : lpfn;
589 }
590 return ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
591}
592
593
594/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 * amdgpu_vce_cs_reloc - command submission relocation
596 *
597 * @p: parser context
598 * @lo: address of lower dword
599 * @hi: address of higher dword
Christian Königf1689ec2015-06-11 20:56:18 +0200600 * @size: minimum size
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601 *
602 * Patch relocation inside command stream with real buffer address
603 */
Christian Königf1689ec2015-06-11 20:56:18 +0200604static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
Christian Königdc783302015-06-12 14:16:20 +0200605 int lo, int hi, unsigned size, uint32_t index)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400606{
607 struct amdgpu_bo_va_mapping *mapping;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 struct amdgpu_bo *bo;
609 uint64_t addr;
Christian König9cca0b82017-09-06 16:15:28 +0200610 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611
Christian Königdc783302015-06-12 14:16:20 +0200612 if (index == 0xffffffff)
613 index = 0;
614
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
616 ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
Christian Königdc783302015-06-12 14:16:20 +0200617 addr += ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618
Christian König9cca0b82017-09-06 16:15:28 +0200619 r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping);
620 if (r) {
Christian Königdc783302015-06-12 14:16:20 +0200621 DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
622 addr, lo, hi, size, index);
Christian König9cca0b82017-09-06 16:15:28 +0200623 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624 }
625
Christian Königf1689ec2015-06-11 20:56:18 +0200626 if ((addr + (uint64_t)size) >
Christian Königa9f87f62017-03-30 14:03:59 +0200627 (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
Christian Königf1689ec2015-06-11 20:56:18 +0200628 DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
629 addr, lo, hi);
630 return -EINVAL;
631 }
632
Christian Königa9f87f62017-03-30 14:03:59 +0200633 addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634 addr += amdgpu_bo_gpu_offset(bo);
Christian Königdc783302015-06-12 14:16:20 +0200635 addr -= ((uint64_t)size) * ((uint64_t)index);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636
Christian König7270f832016-01-31 11:00:41 +0100637 amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
638 amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639
640 return 0;
641}
642
643/**
Christian Königf1689ec2015-06-11 20:56:18 +0200644 * amdgpu_vce_validate_handle - validate stream handle
645 *
646 * @p: parser context
647 * @handle: handle to validate
Christian König2f4b9362015-06-11 21:33:55 +0200648 * @allocated: allocated a new handle?
Christian Königf1689ec2015-06-11 20:56:18 +0200649 *
650 * Validates the handle and return the found session index or -EINVAL
651 * we we don't have another free session index.
652 */
653static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
Christian Könige5223212016-07-01 22:19:25 +0200654 uint32_t handle, uint32_t *allocated)
Christian Königf1689ec2015-06-11 20:56:18 +0200655{
656 unsigned i;
657
658 /* validate the handle */
659 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
Christian König2f4b9362015-06-11 21:33:55 +0200660 if (atomic_read(&p->adev->vce.handles[i]) == handle) {
661 if (p->adev->vce.filp[i] != p->filp) {
662 DRM_ERROR("VCE handle collision detected!\n");
663 return -EINVAL;
664 }
Christian Königf1689ec2015-06-11 20:56:18 +0200665 return i;
Christian König2f4b9362015-06-11 21:33:55 +0200666 }
Christian Königf1689ec2015-06-11 20:56:18 +0200667 }
668
669 /* handle not found try to alloc a new one */
670 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
671 if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
672 p->adev->vce.filp[i] = p->filp;
673 p->adev->vce.img_size[i] = 0;
Christian Könige5223212016-07-01 22:19:25 +0200674 *allocated |= 1 << i;
Christian Königf1689ec2015-06-11 20:56:18 +0200675 return i;
676 }
677 }
678
679 DRM_ERROR("No more free VCE handles!\n");
680 return -EINVAL;
681}
682
683/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 * amdgpu_vce_cs_parse - parse and validate the command stream
685 *
686 * @p: parser context
687 *
688 */
689int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
690{
Christian König50838c82016-02-03 13:44:52 +0100691 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
Christian Königdc783302015-06-12 14:16:20 +0200692 unsigned fb_idx = 0, bs_idx = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200693 int session_idx = -1;
Christian Könige5223212016-07-01 22:19:25 +0200694 uint32_t destroyed = 0;
695 uint32_t created = 0;
696 uint32_t allocated = 0;
Christian Königf1689ec2015-06-11 20:56:18 +0200697 uint32_t tmp, handle = 0;
698 uint32_t *size = &tmp;
Christian König23594312017-11-17 11:09:43 +0100699 unsigned idx;
700 int i, r = 0;
Christian Königc855e252016-09-05 17:00:57 +0200701
Christian König45088ef2016-10-05 16:49:19 +0200702 p->job->vm = NULL;
703 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
704
Christian König23594312017-11-17 11:09:43 +0100705 for (idx = 0; idx < ib->length_dw;) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
707 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
708
709 if ((len < 8) || (len & 3)) {
710 DRM_ERROR("invalid VCE command length (%d)!\n", len);
Christian König2f4b9362015-06-11 21:33:55 +0200711 r = -EINVAL;
712 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 }
714
715 switch (cmd) {
Christian König23594312017-11-17 11:09:43 +0100716 case 0x00000002: /* task info */
717 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
718 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
719 break;
720
721 case 0x03000001: /* encode */
722 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 10,
723 idx + 9, 0, 0);
724 if (r)
725 goto out;
726
727 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 12,
728 idx + 11, 0, 0);
729 if (r)
730 goto out;
731 break;
732
733 case 0x05000001: /* context buffer */
734 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3,
735 idx + 2, 0, 0);
736 if (r)
737 goto out;
738 break;
739
740 case 0x05000004: /* video bitstream buffer */
741 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
742 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3, idx + 2,
743 tmp, bs_idx);
744 if (r)
745 goto out;
746 break;
747
748 case 0x05000005: /* feedback buffer */
749 r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3, idx + 2,
750 4096, fb_idx);
751 if (r)
752 goto out;
753 break;
754 }
755
756 idx += len / 4;
757 }
758
759 for (idx = 0; idx < ib->length_dw;) {
760 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
761 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
762
763 switch (cmd) {
Christian König182830a2016-07-01 17:43:57 +0200764 case 0x00000001: /* session */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
Christian König2f4b9362015-06-11 21:33:55 +0200766 session_idx = amdgpu_vce_validate_handle(p, handle,
767 &allocated);
Christian Könige5223212016-07-01 22:19:25 +0200768 if (session_idx < 0) {
769 r = session_idx;
770 goto out;
771 }
Christian Königf1689ec2015-06-11 20:56:18 +0200772 size = &p->adev->vce.img_size[session_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400773 break;
774
Christian König182830a2016-07-01 17:43:57 +0200775 case 0x00000002: /* task info */
Christian Königdc783302015-06-12 14:16:20 +0200776 fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
777 bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
Christian Königf1689ec2015-06-11 20:56:18 +0200778 break;
779
Christian König182830a2016-07-01 17:43:57 +0200780 case 0x01000001: /* create */
Christian Könige5223212016-07-01 22:19:25 +0200781 created |= 1 << session_idx;
782 if (destroyed & (1 << session_idx)) {
783 destroyed &= ~(1 << session_idx);
784 allocated |= 1 << session_idx;
785
786 } else if (!(allocated & (1 << session_idx))) {
Christian König2f4b9362015-06-11 21:33:55 +0200787 DRM_ERROR("Handle already in use!\n");
788 r = -EINVAL;
789 goto out;
790 }
791
Christian Königf1689ec2015-06-11 20:56:18 +0200792 *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
793 amdgpu_get_ib_value(p, ib_idx, idx + 10) *
794 8 * 3 / 2;
795 break;
796
Christian König182830a2016-07-01 17:43:57 +0200797 case 0x04000001: /* config extension */
798 case 0x04000002: /* pic control */
799 case 0x04000005: /* rate control */
800 case 0x04000007: /* motion estimation */
801 case 0x04000008: /* rdo */
802 case 0x04000009: /* vui */
803 case 0x05000002: /* auxiliary buffer */
Alex Deucher4f827782016-09-21 14:57:06 -0400804 case 0x05000009: /* clock table */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805 break;
806
Alex Deucher5eeda8a2016-09-23 17:22:42 -0400807 case 0x0500000c: /* hw config */
808 switch (p->adev->asic_type) {
809#ifdef CONFIG_DRM_AMDGPU_CIK
810 case CHIP_KAVERI:
811 case CHIP_MULLINS:
812#endif
813 case CHIP_CARRIZO:
814 break;
815 default:
816 r = -EINVAL;
817 goto out;
818 }
819 break;
820
Christian König182830a2016-07-01 17:43:57 +0200821 case 0x03000001: /* encode */
Christian Königf1689ec2015-06-11 20:56:18 +0200822 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
Christian Königdc783302015-06-12 14:16:20 +0200823 *size, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400824 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200825 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400826
Christian Königf1689ec2015-06-11 20:56:18 +0200827 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
Christian Königdc783302015-06-12 14:16:20 +0200828 *size / 3, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200830 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400831 break;
832
Christian König182830a2016-07-01 17:43:57 +0200833 case 0x02000001: /* destroy */
Christian Könige5223212016-07-01 22:19:25 +0200834 destroyed |= 1 << session_idx;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400835 break;
836
Christian König182830a2016-07-01 17:43:57 +0200837 case 0x05000001: /* context buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200838 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200839 *size * 2, 0);
Christian Königf1689ec2015-06-11 20:56:18 +0200840 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200841 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200842 break;
843
Christian König182830a2016-07-01 17:43:57 +0200844 case 0x05000004: /* video bitstream buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200845 tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
846 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200847 tmp, bs_idx);
Christian Königf1689ec2015-06-11 20:56:18 +0200848 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200849 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200850 break;
851
Christian König182830a2016-07-01 17:43:57 +0200852 case 0x05000005: /* feedback buffer */
Christian Königf1689ec2015-06-11 20:56:18 +0200853 r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
Christian Königdc783302015-06-12 14:16:20 +0200854 4096, fb_idx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400855 if (r)
Christian König2f4b9362015-06-11 21:33:55 +0200856 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857 break;
858
859 default:
860 DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
Christian König2f4b9362015-06-11 21:33:55 +0200861 r = -EINVAL;
862 goto out;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400863 }
864
Christian Königf1689ec2015-06-11 20:56:18 +0200865 if (session_idx == -1) {
866 DRM_ERROR("no session command at start of IB\n");
Christian König2f4b9362015-06-11 21:33:55 +0200867 r = -EINVAL;
868 goto out;
Christian Königf1689ec2015-06-11 20:56:18 +0200869 }
870
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400871 idx += len / 4;
872 }
873
Christian Könige5223212016-07-01 22:19:25 +0200874 if (allocated & ~created) {
Christian König2f4b9362015-06-11 21:33:55 +0200875 DRM_ERROR("New session without create command!\n");
876 r = -ENOENT;
877 }
878
879out:
Christian Könige5223212016-07-01 22:19:25 +0200880 if (!r) {
881 /* No error, free all destroyed handle slots */
882 tmp = destroyed;
883 } else {
884 /* Error during parsing, free all allocated handle slots */
885 tmp = allocated;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400886 }
887
Christian Könige5223212016-07-01 22:19:25 +0200888 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
889 if (tmp & (1 << i))
890 atomic_set(&p->adev->vce.handles[i], 0);
891
Christian König2f4b9362015-06-11 21:33:55 +0200892 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400893}
894
895/**
Christian König98614702016-10-10 15:23:32 +0200896 * amdgpu_vce_cs_parse_vm - parse the command stream in VM mode
897 *
898 * @p: parser context
899 *
900 */
901int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, uint32_t ib_idx)
902{
903 struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
904 int session_idx = -1;
905 uint32_t destroyed = 0;
906 uint32_t created = 0;
907 uint32_t allocated = 0;
908 uint32_t tmp, handle = 0;
909 int i, r = 0, idx = 0;
910
911 while (idx < ib->length_dw) {
912 uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
913 uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
914
915 if ((len < 8) || (len & 3)) {
916 DRM_ERROR("invalid VCE command length (%d)!\n", len);
917 r = -EINVAL;
918 goto out;
919 }
920
921 switch (cmd) {
922 case 0x00000001: /* session */
923 handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
924 session_idx = amdgpu_vce_validate_handle(p, handle,
925 &allocated);
926 if (session_idx < 0) {
927 r = session_idx;
928 goto out;
929 }
930 break;
931
932 case 0x01000001: /* create */
933 created |= 1 << session_idx;
934 if (destroyed & (1 << session_idx)) {
935 destroyed &= ~(1 << session_idx);
936 allocated |= 1 << session_idx;
937
938 } else if (!(allocated & (1 << session_idx))) {
939 DRM_ERROR("Handle already in use!\n");
940 r = -EINVAL;
941 goto out;
942 }
943
944 break;
945
946 case 0x02000001: /* destroy */
947 destroyed |= 1 << session_idx;
948 break;
949
950 default:
951 break;
952 }
953
954 if (session_idx == -1) {
955 DRM_ERROR("no session command at start of IB\n");
956 r = -EINVAL;
957 goto out;
958 }
959
960 idx += len / 4;
961 }
962
963 if (allocated & ~created) {
964 DRM_ERROR("New session without create command!\n");
965 r = -ENOENT;
966 }
967
968out:
969 if (!r) {
970 /* No error, free all destroyed handle slots */
971 tmp = destroyed;
972 amdgpu_ib_free(p->adev, ib, NULL);
973 } else {
974 /* Error during parsing, free all allocated handle slots */
975 tmp = allocated;
976 }
977
978 for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
979 if (tmp & (1 << i))
980 atomic_set(&p->adev->vce.handles[i], 0);
981
982 return r;
983}
984
985/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 * amdgpu_vce_ring_emit_ib - execute indirect buffer
987 *
988 * @ring: engine to use
989 * @ib: the IB to execute
990 *
991 */
Christian Königd88bf582016-05-06 17:50:03 +0200992void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib,
993 unsigned vm_id, bool ctx_switch)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400994{
995 amdgpu_ring_write(ring, VCE_CMD_IB);
996 amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
997 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
998 amdgpu_ring_write(ring, ib->length_dw);
999}
1000
1001/**
1002 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
1003 *
1004 * @ring: engine to use
1005 * @fence: the fence
1006 *
1007 */
1008void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +08001009 unsigned flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010{
Chunming Zhou890ee232015-06-01 14:35:03 +08001011 WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001012
1013 amdgpu_ring_write(ring, VCE_CMD_FENCE);
1014 amdgpu_ring_write(ring, addr);
1015 amdgpu_ring_write(ring, upper_32_bits(addr));
1016 amdgpu_ring_write(ring, seq);
1017 amdgpu_ring_write(ring, VCE_CMD_TRAP);
1018 amdgpu_ring_write(ring, VCE_CMD_END);
1019}
1020
1021/**
1022 * amdgpu_vce_ring_test_ring - test if VCE ring is working
1023 *
1024 * @ring: the engine to test on
1025 *
1026 */
1027int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
1028{
1029 struct amdgpu_device *adev = ring->adev;
1030 uint32_t rptr = amdgpu_ring_get_rptr(ring);
1031 unsigned i;
Xiangliang Yua2f537e2017-04-06 14:43:48 +08001032 int r, timeout = adev->usec_timeout;
1033
Frank Mina1b90222017-06-12 11:02:09 +08001034 /* skip ring test for sriov*/
Xiangliang Yua2f537e2017-04-06 14:43:48 +08001035 if (amdgpu_sriov_vf(adev))
Frank Mina1b90222017-06-12 11:02:09 +08001036 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001037
Christian Königa27de352016-01-21 11:28:53 +01001038 r = amdgpu_ring_alloc(ring, 16);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001039 if (r) {
1040 DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
1041 ring->idx, r);
1042 return r;
1043 }
1044 amdgpu_ring_write(ring, VCE_CMD_END);
Christian Königa27de352016-01-21 11:28:53 +01001045 amdgpu_ring_commit(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046
Xiangliang Yua2f537e2017-04-06 14:43:48 +08001047 for (i = 0; i < timeout; i++) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001048 if (amdgpu_ring_get_rptr(ring) != rptr)
1049 break;
1050 DRM_UDELAY(1);
1051 }
1052
Xiangliang Yua2f537e2017-04-06 14:43:48 +08001053 if (i < timeout) {
pding9953b722017-10-26 09:30:38 +08001054 DRM_DEBUG("ring test on %d succeeded in %d usecs\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001055 ring->idx, i);
1056 } else {
1057 DRM_ERROR("amdgpu: ring %d test failed\n",
1058 ring->idx);
1059 r = -ETIMEDOUT;
1060 }
1061
1062 return r;
1063}
1064
1065/**
1066 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
1067 *
1068 * @ring: the engine to test on
1069 *
1070 */
Christian Königbbec97a2016-07-05 21:07:17 +02001071int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072{
Chris Wilsonf54d1862016-10-25 13:00:45 +01001073 struct dma_fence *fence = NULL;
Christian Königbbec97a2016-07-05 21:07:17 +02001074 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001075
Alex Deucher6f0359f2016-08-24 17:15:33 -04001076 /* skip vce ring1/2 ib test for now, since it's not reliable */
1077 if (ring != &ring->adev->vce.ring[0])
Leo Liu898e50d2015-09-04 15:08:55 -04001078 return 0;
1079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080 r = amdgpu_vce_get_create_msg(ring, 1, NULL);
1081 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +02001082 DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083 goto error;
1084 }
1085
Christian König9f2ade32016-02-03 16:50:56 +01001086 r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +02001088 DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001089 goto error;
1090 }
1091
Chris Wilsonf54d1862016-10-25 13:00:45 +01001092 r = dma_fence_wait_timeout(fence, false, timeout);
Christian Königbbec97a2016-07-05 21:07:17 +02001093 if (r == 0) {
1094 DRM_ERROR("amdgpu: IB test timed out.\n");
1095 r = -ETIMEDOUT;
1096 } else if (r < 0) {
1097 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098 } else {
pding9953b722017-10-26 09:30:38 +08001099 DRM_DEBUG("ib test on ring %d succeeded\n", ring->idx);
Christian Königbbec97a2016-07-05 21:07:17 +02001100 r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 }
1102error:
Chris Wilsonf54d1862016-10-25 13:00:45 +01001103 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001104 return r;
1105}