Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 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 */ |
| 39 | #define VCE_IDLE_TIMEOUT_MS 1000 |
| 40 | |
| 41 | /* Firmware Names */ |
| 42 | #ifdef CONFIG_DRM_AMDGPU_CIK |
| 43 | #define FIRMWARE_BONAIRE "radeon/bonaire_vce.bin" |
| 44 | #define FIRMWARE_KABINI "radeon/kabini_vce.bin" |
| 45 | #define FIRMWARE_KAVERI "radeon/kaveri_vce.bin" |
| 46 | #define FIRMWARE_HAWAII "radeon/hawaii_vce.bin" |
| 47 | #define FIRMWARE_MULLINS "radeon/mullins_vce.bin" |
| 48 | #endif |
Jammy Zhou | c65444f | 2015-05-13 22:49:04 +0800 | [diff] [blame] | 49 | #define FIRMWARE_TONGA "amdgpu/tonga_vce.bin" |
| 50 | #define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin" |
Alex Deucher | 188a9bc | 2015-07-27 14:24:14 -0400 | [diff] [blame] | 51 | #define FIRMWARE_FIJI "amdgpu/fiji_vce.bin" |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 52 | |
| 53 | #ifdef CONFIG_DRM_AMDGPU_CIK |
| 54 | MODULE_FIRMWARE(FIRMWARE_BONAIRE); |
| 55 | MODULE_FIRMWARE(FIRMWARE_KABINI); |
| 56 | MODULE_FIRMWARE(FIRMWARE_KAVERI); |
| 57 | MODULE_FIRMWARE(FIRMWARE_HAWAII); |
| 58 | MODULE_FIRMWARE(FIRMWARE_MULLINS); |
| 59 | #endif |
| 60 | MODULE_FIRMWARE(FIRMWARE_TONGA); |
| 61 | MODULE_FIRMWARE(FIRMWARE_CARRIZO); |
Alex Deucher | 188a9bc | 2015-07-27 14:24:14 -0400 | [diff] [blame] | 62 | MODULE_FIRMWARE(FIRMWARE_FIJI); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 63 | |
| 64 | static void amdgpu_vce_idle_work_handler(struct work_struct *work); |
| 65 | |
| 66 | /** |
| 67 | * amdgpu_vce_init - allocate memory, load vce firmware |
| 68 | * |
| 69 | * @adev: amdgpu_device pointer |
| 70 | * |
| 71 | * First step to get VCE online, allocate memory and load the firmware |
| 72 | */ |
Leo Liu | e982262 | 2015-05-06 14:31:27 -0400 | [diff] [blame] | 73 | int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 74 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 75 | const char *fw_name; |
| 76 | const struct common_firmware_header *hdr; |
| 77 | unsigned ucode_version, version_major, version_minor, binary_id; |
| 78 | int i, r; |
| 79 | |
| 80 | INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler); |
| 81 | |
| 82 | switch (adev->asic_type) { |
| 83 | #ifdef CONFIG_DRM_AMDGPU_CIK |
| 84 | case CHIP_BONAIRE: |
| 85 | fw_name = FIRMWARE_BONAIRE; |
| 86 | break; |
| 87 | case CHIP_KAVERI: |
| 88 | fw_name = FIRMWARE_KAVERI; |
| 89 | break; |
| 90 | case CHIP_KABINI: |
| 91 | fw_name = FIRMWARE_KABINI; |
| 92 | break; |
| 93 | case CHIP_HAWAII: |
| 94 | fw_name = FIRMWARE_HAWAII; |
| 95 | break; |
| 96 | case CHIP_MULLINS: |
| 97 | fw_name = FIRMWARE_MULLINS; |
| 98 | break; |
| 99 | #endif |
| 100 | case CHIP_TONGA: |
| 101 | fw_name = FIRMWARE_TONGA; |
| 102 | break; |
| 103 | case CHIP_CARRIZO: |
| 104 | fw_name = FIRMWARE_CARRIZO; |
| 105 | break; |
Alex Deucher | 188a9bc | 2015-07-27 14:24:14 -0400 | [diff] [blame] | 106 | case CHIP_FIJI: |
| 107 | fw_name = FIRMWARE_FIJI; |
| 108 | break; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 109 | |
| 110 | default: |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | r = request_firmware(&adev->vce.fw, fw_name, adev->dev); |
| 115 | if (r) { |
| 116 | dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n", |
| 117 | fw_name); |
| 118 | return r; |
| 119 | } |
| 120 | |
| 121 | r = amdgpu_ucode_validate(adev->vce.fw); |
| 122 | if (r) { |
| 123 | dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n", |
| 124 | fw_name); |
| 125 | release_firmware(adev->vce.fw); |
| 126 | adev->vce.fw = NULL; |
| 127 | return r; |
| 128 | } |
| 129 | |
| 130 | hdr = (const struct common_firmware_header *)adev->vce.fw->data; |
| 131 | |
| 132 | ucode_version = le32_to_cpu(hdr->ucode_version); |
| 133 | version_major = (ucode_version >> 20) & 0xfff; |
| 134 | version_minor = (ucode_version >> 8) & 0xfff; |
| 135 | binary_id = ucode_version & 0xff; |
| 136 | DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n", |
| 137 | version_major, version_minor, binary_id); |
| 138 | adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) | |
| 139 | (binary_id << 8)); |
| 140 | |
| 141 | /* allocate firmware, stack and heap BO */ |
| 142 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 143 | r = amdgpu_bo_create(adev, size, PAGE_SIZE, true, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame^] | 144 | AMDGPU_GEM_DOMAIN_VRAM, |
| 145 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, |
| 146 | NULL, &adev->vce.vcpu_bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 147 | if (r) { |
| 148 | dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r); |
| 149 | return r; |
| 150 | } |
| 151 | |
| 152 | r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false); |
| 153 | if (r) { |
| 154 | amdgpu_bo_unref(&adev->vce.vcpu_bo); |
| 155 | dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r); |
| 156 | return r; |
| 157 | } |
| 158 | |
| 159 | r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM, |
| 160 | &adev->vce.gpu_addr); |
| 161 | amdgpu_bo_unreserve(adev->vce.vcpu_bo); |
| 162 | if (r) { |
| 163 | amdgpu_bo_unref(&adev->vce.vcpu_bo); |
| 164 | dev_err(adev->dev, "(%d) VCE bo pin failed\n", r); |
| 165 | return r; |
| 166 | } |
| 167 | |
| 168 | for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { |
| 169 | atomic_set(&adev->vce.handles[i], 0); |
| 170 | adev->vce.filp[i] = NULL; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * amdgpu_vce_fini - free memory |
| 178 | * |
| 179 | * @adev: amdgpu_device pointer |
| 180 | * |
| 181 | * Last step on VCE teardown, free firmware memory |
| 182 | */ |
| 183 | int amdgpu_vce_sw_fini(struct amdgpu_device *adev) |
| 184 | { |
| 185 | if (adev->vce.vcpu_bo == NULL) |
| 186 | return 0; |
| 187 | |
| 188 | amdgpu_bo_unref(&adev->vce.vcpu_bo); |
| 189 | |
| 190 | amdgpu_ring_fini(&adev->vce.ring[0]); |
| 191 | amdgpu_ring_fini(&adev->vce.ring[1]); |
| 192 | |
| 193 | release_firmware(adev->vce.fw); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * amdgpu_vce_suspend - unpin VCE fw memory |
| 200 | * |
| 201 | * @adev: amdgpu_device pointer |
| 202 | * |
| 203 | */ |
| 204 | int amdgpu_vce_suspend(struct amdgpu_device *adev) |
| 205 | { |
| 206 | int i; |
| 207 | |
| 208 | if (adev->vce.vcpu_bo == NULL) |
| 209 | return 0; |
| 210 | |
| 211 | for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) |
| 212 | if (atomic_read(&adev->vce.handles[i])) |
| 213 | break; |
| 214 | |
| 215 | if (i == AMDGPU_MAX_VCE_HANDLES) |
| 216 | return 0; |
| 217 | |
| 218 | /* TODO: suspending running encoding sessions isn't supported */ |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * amdgpu_vce_resume - pin VCE fw memory |
| 224 | * |
| 225 | * @adev: amdgpu_device pointer |
| 226 | * |
| 227 | */ |
| 228 | int amdgpu_vce_resume(struct amdgpu_device *adev) |
| 229 | { |
| 230 | void *cpu_addr; |
| 231 | const struct common_firmware_header *hdr; |
| 232 | unsigned offset; |
| 233 | int r; |
| 234 | |
| 235 | if (adev->vce.vcpu_bo == NULL) |
| 236 | return -EINVAL; |
| 237 | |
| 238 | r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false); |
| 239 | if (r) { |
| 240 | dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r); |
| 241 | return r; |
| 242 | } |
| 243 | |
| 244 | r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr); |
| 245 | if (r) { |
| 246 | amdgpu_bo_unreserve(adev->vce.vcpu_bo); |
| 247 | dev_err(adev->dev, "(%d) VCE map failed\n", r); |
| 248 | return r; |
| 249 | } |
| 250 | |
| 251 | hdr = (const struct common_firmware_header *)adev->vce.fw->data; |
| 252 | offset = le32_to_cpu(hdr->ucode_array_offset_bytes); |
| 253 | memcpy(cpu_addr, (adev->vce.fw->data) + offset, |
| 254 | (adev->vce.fw->size) - offset); |
| 255 | |
| 256 | amdgpu_bo_kunmap(adev->vce.vcpu_bo); |
| 257 | |
| 258 | amdgpu_bo_unreserve(adev->vce.vcpu_bo); |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * amdgpu_vce_idle_work_handler - power off VCE |
| 265 | * |
| 266 | * @work: pointer to work structure |
| 267 | * |
| 268 | * power of VCE when it's not used any more |
| 269 | */ |
| 270 | static void amdgpu_vce_idle_work_handler(struct work_struct *work) |
| 271 | { |
| 272 | struct amdgpu_device *adev = |
| 273 | container_of(work, struct amdgpu_device, vce.idle_work.work); |
| 274 | |
| 275 | if ((amdgpu_fence_count_emitted(&adev->vce.ring[0]) == 0) && |
| 276 | (amdgpu_fence_count_emitted(&adev->vce.ring[1]) == 0)) { |
| 277 | if (adev->pm.dpm_enabled) { |
| 278 | amdgpu_dpm_enable_vce(adev, false); |
| 279 | } else { |
| 280 | amdgpu_asic_set_vce_clocks(adev, 0, 0); |
| 281 | } |
| 282 | } else { |
| 283 | schedule_delayed_work(&adev->vce.idle_work, |
| 284 | msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS)); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * amdgpu_vce_note_usage - power up VCE |
| 290 | * |
| 291 | * @adev: amdgpu_device pointer |
| 292 | * |
| 293 | * Make sure VCE is powerd up when we want to use it |
| 294 | */ |
| 295 | static void amdgpu_vce_note_usage(struct amdgpu_device *adev) |
| 296 | { |
| 297 | bool streams_changed = false; |
| 298 | bool set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work); |
| 299 | set_clocks &= schedule_delayed_work(&adev->vce.idle_work, |
| 300 | msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS)); |
| 301 | |
| 302 | if (adev->pm.dpm_enabled) { |
| 303 | /* XXX figure out if the streams changed */ |
| 304 | streams_changed = false; |
| 305 | } |
| 306 | |
| 307 | if (set_clocks || streams_changed) { |
| 308 | if (adev->pm.dpm_enabled) { |
| 309 | amdgpu_dpm_enable_vce(adev, true); |
| 310 | } else { |
| 311 | amdgpu_asic_set_vce_clocks(adev, 53300, 40000); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * amdgpu_vce_free_handles - free still open VCE handles |
| 318 | * |
| 319 | * @adev: amdgpu_device pointer |
| 320 | * @filp: drm file pointer |
| 321 | * |
| 322 | * Close all VCE handles still open by this file pointer |
| 323 | */ |
| 324 | void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp) |
| 325 | { |
| 326 | struct amdgpu_ring *ring = &adev->vce.ring[0]; |
| 327 | int i, r; |
| 328 | for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { |
| 329 | uint32_t handle = atomic_read(&adev->vce.handles[i]); |
| 330 | if (!handle || adev->vce.filp[i] != filp) |
| 331 | continue; |
| 332 | |
| 333 | amdgpu_vce_note_usage(adev); |
| 334 | |
| 335 | r = amdgpu_vce_get_destroy_msg(ring, handle, NULL); |
| 336 | if (r) |
| 337 | DRM_ERROR("Error destroying VCE handle (%d)!\n", r); |
| 338 | |
| 339 | adev->vce.filp[i] = NULL; |
| 340 | atomic_set(&adev->vce.handles[i], 0); |
| 341 | } |
| 342 | } |
| 343 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 344 | static int amdgpu_vce_free_job( |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 345 | struct amdgpu_job *sched_job) |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 346 | { |
| 347 | amdgpu_ib_free(sched_job->adev, sched_job->ibs); |
| 348 | kfree(sched_job->ibs); |
| 349 | return 0; |
| 350 | } |
| 351 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 352 | /** |
| 353 | * amdgpu_vce_get_create_msg - generate a VCE create msg |
| 354 | * |
| 355 | * @adev: amdgpu_device pointer |
| 356 | * @ring: ring we should submit the msg to |
| 357 | * @handle: VCE session handle to use |
| 358 | * @fence: optional fence to return |
| 359 | * |
| 360 | * Open up a stream for HW test |
| 361 | */ |
| 362 | int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle, |
Chunming Zhou | ed40bfb | 2015-08-03 13:28:16 +0800 | [diff] [blame] | 363 | struct fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 364 | { |
| 365 | const unsigned ib_size_dw = 1024; |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 366 | struct amdgpu_ib *ib = NULL; |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 367 | struct fence *f = NULL; |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 368 | struct amdgpu_device *adev = ring->adev; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 369 | uint64_t dummy; |
| 370 | int i, r; |
| 371 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 372 | ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); |
| 373 | if (!ib) |
| 374 | return -ENOMEM; |
| 375 | r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 376 | if (r) { |
| 377 | DRM_ERROR("amdgpu: failed to get ib (%d).\n", r); |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 378 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 379 | return r; |
| 380 | } |
| 381 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 382 | dummy = ib->gpu_addr + 1024; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 383 | |
| 384 | /* stitch together an VCE create msg */ |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 385 | ib->length_dw = 0; |
| 386 | ib->ptr[ib->length_dw++] = 0x0000000c; /* len */ |
| 387 | ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */ |
| 388 | ib->ptr[ib->length_dw++] = handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 389 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 390 | ib->ptr[ib->length_dw++] = 0x00000030; /* len */ |
| 391 | ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */ |
| 392 | ib->ptr[ib->length_dw++] = 0x00000000; |
| 393 | ib->ptr[ib->length_dw++] = 0x00000042; |
| 394 | ib->ptr[ib->length_dw++] = 0x0000000a; |
| 395 | ib->ptr[ib->length_dw++] = 0x00000001; |
| 396 | ib->ptr[ib->length_dw++] = 0x00000080; |
| 397 | ib->ptr[ib->length_dw++] = 0x00000060; |
| 398 | ib->ptr[ib->length_dw++] = 0x00000100; |
| 399 | ib->ptr[ib->length_dw++] = 0x00000100; |
| 400 | ib->ptr[ib->length_dw++] = 0x0000000c; |
| 401 | ib->ptr[ib->length_dw++] = 0x00000000; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 402 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 403 | ib->ptr[ib->length_dw++] = 0x00000014; /* len */ |
| 404 | ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */ |
| 405 | ib->ptr[ib->length_dw++] = upper_32_bits(dummy); |
| 406 | ib->ptr[ib->length_dw++] = dummy; |
| 407 | ib->ptr[ib->length_dw++] = 0x00000001; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 408 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 409 | for (i = ib->length_dw; i < ib_size_dw; ++i) |
| 410 | ib->ptr[i] = 0x0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 411 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 412 | r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, |
| 413 | &amdgpu_vce_free_job, |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 414 | AMDGPU_FENCE_OWNER_UNDEFINED, |
| 415 | &f); |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 416 | if (r) |
| 417 | goto err; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 418 | if (fence) |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 419 | *fence = fence_get(f); |
Chunming Zhou | 281b422 | 2015-08-12 12:58:31 +0800 | [diff] [blame] | 420 | fence_put(f); |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 421 | if (amdgpu_enable_scheduler) |
| 422 | return 0; |
| 423 | err: |
| 424 | amdgpu_ib_free(adev, ib); |
| 425 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 426 | return r; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg |
| 431 | * |
| 432 | * @adev: amdgpu_device pointer |
| 433 | * @ring: ring we should submit the msg to |
| 434 | * @handle: VCE session handle to use |
| 435 | * @fence: optional fence to return |
| 436 | * |
| 437 | * Close up a stream for HW test or if userspace failed to do so |
| 438 | */ |
| 439 | int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle, |
Chunming Zhou | ed40bfb | 2015-08-03 13:28:16 +0800 | [diff] [blame] | 440 | struct fence **fence) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 441 | { |
| 442 | const unsigned ib_size_dw = 1024; |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 443 | struct amdgpu_ib *ib = NULL; |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 444 | struct fence *f = NULL; |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 445 | struct amdgpu_device *adev = ring->adev; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 446 | uint64_t dummy; |
| 447 | int i, r; |
| 448 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 449 | ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); |
| 450 | if (!ib) |
| 451 | return -ENOMEM; |
| 452 | |
| 453 | r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 454 | if (r) { |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 455 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 456 | DRM_ERROR("amdgpu: failed to get ib (%d).\n", r); |
| 457 | return r; |
| 458 | } |
| 459 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 460 | dummy = ib->gpu_addr + 1024; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 461 | |
| 462 | /* stitch together an VCE destroy msg */ |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 463 | ib->length_dw = 0; |
| 464 | ib->ptr[ib->length_dw++] = 0x0000000c; /* len */ |
| 465 | ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */ |
| 466 | ib->ptr[ib->length_dw++] = handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 467 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 468 | ib->ptr[ib->length_dw++] = 0x00000014; /* len */ |
| 469 | ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */ |
| 470 | ib->ptr[ib->length_dw++] = upper_32_bits(dummy); |
| 471 | ib->ptr[ib->length_dw++] = dummy; |
| 472 | ib->ptr[ib->length_dw++] = 0x00000001; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 473 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 474 | ib->ptr[ib->length_dw++] = 0x00000008; /* len */ |
| 475 | ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 476 | |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 477 | for (i = ib->length_dw; i < ib_size_dw; ++i) |
| 478 | ib->ptr[i] = 0x0; |
| 479 | r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, |
| 480 | &amdgpu_vce_free_job, |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 481 | AMDGPU_FENCE_OWNER_UNDEFINED, |
| 482 | &f); |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 483 | if (r) |
| 484 | goto err; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 485 | if (fence) |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 486 | *fence = fence_get(f); |
Chunming Zhou | 281b422 | 2015-08-12 12:58:31 +0800 | [diff] [blame] | 487 | fence_put(f); |
Chunming Zhou | 8128765 | 2015-07-03 14:18:26 +0800 | [diff] [blame] | 488 | if (amdgpu_enable_scheduler) |
| 489 | return 0; |
| 490 | err: |
| 491 | amdgpu_ib_free(adev, ib); |
| 492 | kfree(ib); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 493 | return r; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * amdgpu_vce_cs_reloc - command submission relocation |
| 498 | * |
| 499 | * @p: parser context |
| 500 | * @lo: address of lower dword |
| 501 | * @hi: address of higher dword |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 502 | * @size: minimum size |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 503 | * |
| 504 | * Patch relocation inside command stream with real buffer address |
| 505 | */ |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 506 | static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx, |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 507 | int lo, int hi, unsigned size, uint32_t index) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 508 | { |
| 509 | struct amdgpu_bo_va_mapping *mapping; |
| 510 | struct amdgpu_ib *ib = &p->ibs[ib_idx]; |
| 511 | struct amdgpu_bo *bo; |
| 512 | uint64_t addr; |
| 513 | |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 514 | if (index == 0xffffffff) |
| 515 | index = 0; |
| 516 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 517 | addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) | |
| 518 | ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32; |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 519 | addr += ((uint64_t)size) * ((uint64_t)index); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 520 | |
| 521 | mapping = amdgpu_cs_find_mapping(p, addr, &bo); |
| 522 | if (mapping == NULL) { |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 523 | DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n", |
| 524 | addr, lo, hi, size, index); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 525 | return -EINVAL; |
| 526 | } |
| 527 | |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 528 | if ((addr + (uint64_t)size) > |
| 529 | ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) { |
| 530 | DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n", |
| 531 | addr, lo, hi); |
| 532 | return -EINVAL; |
| 533 | } |
| 534 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 535 | addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE; |
| 536 | addr += amdgpu_bo_gpu_offset(bo); |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 537 | addr -= ((uint64_t)size) * ((uint64_t)index); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 538 | |
| 539 | ib->ptr[lo] = addr & 0xFFFFFFFF; |
| 540 | ib->ptr[hi] = addr >> 32; |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /** |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 546 | * amdgpu_vce_validate_handle - validate stream handle |
| 547 | * |
| 548 | * @p: parser context |
| 549 | * @handle: handle to validate |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 550 | * @allocated: allocated a new handle? |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 551 | * |
| 552 | * Validates the handle and return the found session index or -EINVAL |
| 553 | * we we don't have another free session index. |
| 554 | */ |
| 555 | static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p, |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 556 | uint32_t handle, bool *allocated) |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 557 | { |
| 558 | unsigned i; |
| 559 | |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 560 | *allocated = false; |
| 561 | |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 562 | /* validate the handle */ |
| 563 | for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 564 | if (atomic_read(&p->adev->vce.handles[i]) == handle) { |
| 565 | if (p->adev->vce.filp[i] != p->filp) { |
| 566 | DRM_ERROR("VCE handle collision detected!\n"); |
| 567 | return -EINVAL; |
| 568 | } |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 569 | return i; |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 570 | } |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* handle not found try to alloc a new one */ |
| 574 | for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) { |
| 575 | if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) { |
| 576 | p->adev->vce.filp[i] = p->filp; |
| 577 | p->adev->vce.img_size[i] = 0; |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 578 | *allocated = true; |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 579 | return i; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | DRM_ERROR("No more free VCE handles!\n"); |
| 584 | return -EINVAL; |
| 585 | } |
| 586 | |
| 587 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 588 | * amdgpu_vce_cs_parse - parse and validate the command stream |
| 589 | * |
| 590 | * @p: parser context |
| 591 | * |
| 592 | */ |
| 593 | int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx) |
| 594 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 595 | struct amdgpu_ib *ib = &p->ibs[ib_idx]; |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 596 | unsigned fb_idx = 0, bs_idx = 0; |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 597 | int session_idx = -1; |
| 598 | bool destroyed = false; |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 599 | bool created = false; |
| 600 | bool allocated = false; |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 601 | uint32_t tmp, handle = 0; |
| 602 | uint32_t *size = &tmp; |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 603 | int i, r = 0, idx = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 604 | |
| 605 | amdgpu_vce_note_usage(p->adev); |
| 606 | |
| 607 | while (idx < ib->length_dw) { |
| 608 | uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx); |
| 609 | uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1); |
| 610 | |
| 611 | if ((len < 8) || (len & 3)) { |
| 612 | DRM_ERROR("invalid VCE command length (%d)!\n", len); |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 613 | r = -EINVAL; |
| 614 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 615 | } |
| 616 | |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 617 | if (destroyed) { |
| 618 | DRM_ERROR("No other command allowed after destroy!\n"); |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 619 | r = -EINVAL; |
| 620 | goto out; |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 623 | switch (cmd) { |
| 624 | case 0x00000001: // session |
| 625 | handle = amdgpu_get_ib_value(p, ib_idx, idx + 2); |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 626 | session_idx = amdgpu_vce_validate_handle(p, handle, |
| 627 | &allocated); |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 628 | if (session_idx < 0) |
| 629 | return session_idx; |
| 630 | size = &p->adev->vce.img_size[session_idx]; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 631 | break; |
| 632 | |
| 633 | case 0x00000002: // task info |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 634 | fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6); |
| 635 | bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7); |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 636 | break; |
| 637 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 638 | case 0x01000001: // create |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 639 | created = true; |
| 640 | if (!allocated) { |
| 641 | DRM_ERROR("Handle already in use!\n"); |
| 642 | r = -EINVAL; |
| 643 | goto out; |
| 644 | } |
| 645 | |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 646 | *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) * |
| 647 | amdgpu_get_ib_value(p, ib_idx, idx + 10) * |
| 648 | 8 * 3 / 2; |
| 649 | break; |
| 650 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 651 | case 0x04000001: // config extension |
| 652 | case 0x04000002: // pic control |
| 653 | case 0x04000005: // rate control |
| 654 | case 0x04000007: // motion estimation |
| 655 | case 0x04000008: // rdo |
| 656 | case 0x04000009: // vui |
| 657 | case 0x05000002: // auxiliary buffer |
| 658 | break; |
| 659 | |
| 660 | case 0x03000001: // encode |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 661 | r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9, |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 662 | *size, 0); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 663 | if (r) |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 664 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 665 | |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 666 | r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11, |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 667 | *size / 3, 0); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 668 | if (r) |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 669 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 670 | break; |
| 671 | |
| 672 | case 0x02000001: // destroy |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 673 | destroyed = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 674 | break; |
| 675 | |
| 676 | case 0x05000001: // context buffer |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 677 | r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2, |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 678 | *size * 2, 0); |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 679 | if (r) |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 680 | goto out; |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 681 | break; |
| 682 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 683 | case 0x05000004: // video bitstream buffer |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 684 | tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4); |
| 685 | r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2, |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 686 | tmp, bs_idx); |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 687 | if (r) |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 688 | goto out; |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 689 | break; |
| 690 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 691 | case 0x05000005: // feedback buffer |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 692 | r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2, |
Christian König | dc78330 | 2015-06-12 14:16:20 +0200 | [diff] [blame] | 693 | 4096, fb_idx); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 694 | if (r) |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 695 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 696 | break; |
| 697 | |
| 698 | default: |
| 699 | DRM_ERROR("invalid VCE command (0x%x)!\n", cmd); |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 700 | r = -EINVAL; |
| 701 | goto out; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 702 | } |
| 703 | |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 704 | if (session_idx == -1) { |
| 705 | DRM_ERROR("no session command at start of IB\n"); |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 706 | r = -EINVAL; |
| 707 | goto out; |
Christian König | f1689ec | 2015-06-11 20:56:18 +0200 | [diff] [blame] | 708 | } |
| 709 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 710 | idx += len / 4; |
| 711 | } |
| 712 | |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 713 | if (allocated && !created) { |
| 714 | DRM_ERROR("New session without create command!\n"); |
| 715 | r = -ENOENT; |
| 716 | } |
| 717 | |
| 718 | out: |
| 719 | if ((!r && destroyed) || (r && allocated)) { |
| 720 | /* |
| 721 | * IB contains a destroy msg or we have allocated an |
| 722 | * handle and got an error, anyway free the handle |
| 723 | */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 724 | for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) |
| 725 | atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 726 | } |
| 727 | |
Christian König | 2f4b936 | 2015-06-11 21:33:55 +0200 | [diff] [blame] | 728 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /** |
| 732 | * amdgpu_vce_ring_emit_semaphore - emit a semaphore command |
| 733 | * |
| 734 | * @ring: engine to use |
| 735 | * @semaphore: address of semaphore |
| 736 | * @emit_wait: true=emit wait, false=emit signal |
| 737 | * |
| 738 | */ |
| 739 | bool amdgpu_vce_ring_emit_semaphore(struct amdgpu_ring *ring, |
| 740 | struct amdgpu_semaphore *semaphore, |
| 741 | bool emit_wait) |
| 742 | { |
| 743 | uint64_t addr = semaphore->gpu_addr; |
| 744 | |
| 745 | amdgpu_ring_write(ring, VCE_CMD_SEMAPHORE); |
| 746 | amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF); |
| 747 | amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF); |
| 748 | amdgpu_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0)); |
| 749 | if (!emit_wait) |
| 750 | amdgpu_ring_write(ring, VCE_CMD_END); |
| 751 | |
| 752 | return true; |
| 753 | } |
| 754 | |
| 755 | /** |
| 756 | * amdgpu_vce_ring_emit_ib - execute indirect buffer |
| 757 | * |
| 758 | * @ring: engine to use |
| 759 | * @ib: the IB to execute |
| 760 | * |
| 761 | */ |
| 762 | void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib) |
| 763 | { |
| 764 | amdgpu_ring_write(ring, VCE_CMD_IB); |
| 765 | amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr)); |
| 766 | amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr)); |
| 767 | amdgpu_ring_write(ring, ib->length_dw); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * amdgpu_vce_ring_emit_fence - add a fence command to the ring |
| 772 | * |
| 773 | * @ring: engine to use |
| 774 | * @fence: the fence |
| 775 | * |
| 776 | */ |
| 777 | void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq, |
Chunming Zhou | 890ee23 | 2015-06-01 14:35:03 +0800 | [diff] [blame] | 778 | unsigned flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 779 | { |
Chunming Zhou | 890ee23 | 2015-06-01 14:35:03 +0800 | [diff] [blame] | 780 | WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 781 | |
| 782 | amdgpu_ring_write(ring, VCE_CMD_FENCE); |
| 783 | amdgpu_ring_write(ring, addr); |
| 784 | amdgpu_ring_write(ring, upper_32_bits(addr)); |
| 785 | amdgpu_ring_write(ring, seq); |
| 786 | amdgpu_ring_write(ring, VCE_CMD_TRAP); |
| 787 | amdgpu_ring_write(ring, VCE_CMD_END); |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * amdgpu_vce_ring_test_ring - test if VCE ring is working |
| 792 | * |
| 793 | * @ring: the engine to test on |
| 794 | * |
| 795 | */ |
| 796 | int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring) |
| 797 | { |
| 798 | struct amdgpu_device *adev = ring->adev; |
| 799 | uint32_t rptr = amdgpu_ring_get_rptr(ring); |
| 800 | unsigned i; |
| 801 | int r; |
| 802 | |
| 803 | r = amdgpu_ring_lock(ring, 16); |
| 804 | if (r) { |
| 805 | DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n", |
| 806 | ring->idx, r); |
| 807 | return r; |
| 808 | } |
| 809 | amdgpu_ring_write(ring, VCE_CMD_END); |
| 810 | amdgpu_ring_unlock_commit(ring); |
| 811 | |
| 812 | for (i = 0; i < adev->usec_timeout; i++) { |
| 813 | if (amdgpu_ring_get_rptr(ring) != rptr) |
| 814 | break; |
| 815 | DRM_UDELAY(1); |
| 816 | } |
| 817 | |
| 818 | if (i < adev->usec_timeout) { |
| 819 | DRM_INFO("ring test on %d succeeded in %d usecs\n", |
| 820 | ring->idx, i); |
| 821 | } else { |
| 822 | DRM_ERROR("amdgpu: ring %d test failed\n", |
| 823 | ring->idx); |
| 824 | r = -ETIMEDOUT; |
| 825 | } |
| 826 | |
| 827 | return r; |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * amdgpu_vce_ring_test_ib - test if VCE IBs are working |
| 832 | * |
| 833 | * @ring: the engine to test on |
| 834 | * |
| 835 | */ |
| 836 | int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring) |
| 837 | { |
Chunming Zhou | ed40bfb | 2015-08-03 13:28:16 +0800 | [diff] [blame] | 838 | struct fence *fence = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 839 | int r; |
| 840 | |
| 841 | r = amdgpu_vce_get_create_msg(ring, 1, NULL); |
| 842 | if (r) { |
| 843 | DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r); |
| 844 | goto error; |
| 845 | } |
| 846 | |
| 847 | r = amdgpu_vce_get_destroy_msg(ring, 1, &fence); |
| 848 | if (r) { |
| 849 | DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r); |
| 850 | goto error; |
| 851 | } |
| 852 | |
Chunming Zhou | ed40bfb | 2015-08-03 13:28:16 +0800 | [diff] [blame] | 853 | r = fence_wait(fence, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 854 | if (r) { |
| 855 | DRM_ERROR("amdgpu: fence wait failed (%d).\n", r); |
| 856 | } else { |
| 857 | DRM_INFO("ib test on ring %d succeeded\n", ring->idx); |
| 858 | } |
| 859 | error: |
Chunming Zhou | ed40bfb | 2015-08-03 13:28:16 +0800 | [diff] [blame] | 860 | fence_put(fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 861 | return r; |
| 862 | } |