Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Advanced Micro Devices, Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * |
| 23 | */ |
| 24 | #include <linux/kthread.h> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <drm/drmP.h> |
| 28 | #include "amdgpu.h" |
Chunming Zhou | 7034dec | 2015-11-11 14:56:00 +0800 | [diff] [blame] | 29 | #include "amdgpu_trace.h" |
Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 30 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 31 | static struct fence *amdgpu_sched_dependency(struct amd_sched_job *sched_job) |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 32 | { |
Junwei Zhang | a6db8a3 | 2015-09-09 09:21:19 +0800 | [diff] [blame] | 33 | struct amdgpu_job *job = to_amdgpu_job(sched_job); |
Christian König | 8d0a7ce | 2015-11-03 20:58:50 +0100 | [diff] [blame] | 34 | struct amdgpu_sync *sync = &job->ibs->sync; |
| 35 | struct amdgpu_vm *vm = job->ibs->vm; |
| 36 | |
| 37 | struct fence *fence = amdgpu_sync_get_fence(sync); |
| 38 | |
| 39 | if (fence == NULL && vm && !job->ibs->grabbed_vmid) { |
| 40 | struct amdgpu_ring *ring = job->ibs->ring; |
Christian König | 8d0a7ce | 2015-11-03 20:58:50 +0100 | [diff] [blame] | 41 | int r; |
| 42 | |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame^] | 43 | r = amdgpu_vm_grab_id(vm, ring, sync, |
| 44 | &job->base.s_fence->base); |
| 45 | if (r) |
Christian König | 8d0a7ce | 2015-11-03 20:58:50 +0100 | [diff] [blame] | 46 | DRM_ERROR("Error getting VM ID (%d)\n", r); |
Christian König | 94dd0a4 | 2016-01-18 17:01:42 +0100 | [diff] [blame^] | 47 | else |
Christian König | 8d0a7ce | 2015-11-03 20:58:50 +0100 | [diff] [blame] | 48 | job->ibs->grabbed_vmid = true; |
Christian König | 8d0a7ce | 2015-11-03 20:58:50 +0100 | [diff] [blame] | 49 | |
| 50 | fence = amdgpu_sync_get_fence(sync); |
| 51 | } |
| 52 | |
| 53 | return fence; |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 56 | static struct fence *amdgpu_sched_run_job(struct amd_sched_job *sched_job) |
Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 57 | { |
Christian König | 1886d1a | 2015-08-31 17:28:28 +0200 | [diff] [blame] | 58 | struct amdgpu_fence *fence = NULL; |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 59 | struct amdgpu_job *job; |
Christian König | bd755d0 | 2015-08-24 14:57:26 +0200 | [diff] [blame] | 60 | int r; |
Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 61 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 62 | if (!sched_job) { |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 63 | DRM_ERROR("job is null\n"); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 64 | return NULL; |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 65 | } |
Junwei Zhang | a6db8a3 | 2015-09-09 09:21:19 +0800 | [diff] [blame] | 66 | job = to_amdgpu_job(sched_job); |
Chunming Zhou | 7034dec | 2015-11-11 14:56:00 +0800 | [diff] [blame] | 67 | trace_amdgpu_sched_run_job(job); |
Christian König | e284022 | 2015-11-05 19:49:48 +0100 | [diff] [blame] | 68 | r = amdgpu_ib_schedule(job->adev, job->num_ibs, job->ibs, job->owner); |
Christian König | 1886d1a | 2015-08-31 17:28:28 +0200 | [diff] [blame] | 69 | if (r) { |
| 70 | DRM_ERROR("Error scheduling IBs (%d)\n", r); |
Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 71 | goto err; |
Christian König | 1886d1a | 2015-08-31 17:28:28 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Christian König | 6ef68c1 | 2015-10-22 15:16:22 +0200 | [diff] [blame] | 74 | fence = job->ibs[job->num_ibs - 1].fence; |
| 75 | fence_get(&fence->base); |
Chunming Zhou | 7484667 | 2015-08-04 11:30:09 +0800 | [diff] [blame] | 76 | |
Christian König | 1886d1a | 2015-08-31 17:28:28 +0200 | [diff] [blame] | 77 | err: |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 78 | if (job->free_job) |
| 79 | job->free_job(job); |
Christian König | bf7ebae | 2015-08-18 15:30:26 +0200 | [diff] [blame] | 80 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 81 | kfree(job); |
Christian König | 1886d1a | 2015-08-31 17:28:28 +0200 | [diff] [blame] | 82 | return fence ? &fence->base : NULL; |
Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | struct amd_sched_backend_ops amdgpu_sched_ops = { |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 86 | .dependency = amdgpu_sched_dependency, |
Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 87 | .run_job = amdgpu_sched_run_job, |
Chunming Zhou | c1b69ed | 2015-07-21 13:45:14 +0800 | [diff] [blame] | 88 | }; |
| 89 | |
Chunming Zhou | 3c704e9 | 2015-07-29 10:33:14 +0800 | [diff] [blame] | 90 | int amdgpu_sched_ib_submit_kernel_helper(struct amdgpu_device *adev, |
| 91 | struct amdgpu_ring *ring, |
| 92 | struct amdgpu_ib *ibs, |
| 93 | unsigned num_ibs, |
Chunming Zhou | bb977d3 | 2015-08-18 15:16:40 +0800 | [diff] [blame] | 94 | int (*free_job)(struct amdgpu_job *), |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 95 | void *owner, |
| 96 | struct fence **f) |
Chunming Zhou | 3c704e9 | 2015-07-29 10:33:14 +0800 | [diff] [blame] | 97 | { |
Chunming Zhou | cadf97b | 2016-01-15 11:25:00 +0800 | [diff] [blame] | 98 | struct amdgpu_job *job = |
| 99 | kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL); |
| 100 | if (!job) |
| 101 | return -ENOMEM; |
| 102 | job->base.sched = &ring->sched; |
| 103 | job->base.s_entity = &adev->kernel_ctx.rings[ring->idx].entity; |
| 104 | job->base.s_fence = amd_sched_fence_create(job->base.s_entity, owner); |
| 105 | if (!job->base.s_fence) { |
| 106 | kfree(job); |
| 107 | return -ENOMEM; |
Chunming Zhou | f556cb0c | 2015-08-02 11:18:04 +0800 | [diff] [blame] | 108 | } |
Chunming Zhou | cadf97b | 2016-01-15 11:25:00 +0800 | [diff] [blame] | 109 | *f = fence_get(&job->base.s_fence->base); |
| 110 | |
| 111 | job->adev = adev; |
| 112 | job->ibs = ibs; |
| 113 | job->num_ibs = num_ibs; |
| 114 | job->owner = owner; |
| 115 | job->free_job = free_job; |
| 116 | amd_sched_entity_push_job(&job->base); |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 117 | |
Chunming Zhou | 1763552 | 2015-08-03 11:43:19 +0800 | [diff] [blame] | 118 | return 0; |
Chunming Zhou | 3c704e9 | 2015-07-29 10:33:14 +0800 | [diff] [blame] | 119 | } |