Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +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 | * |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 22 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 23 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 24 | #include <linux/kthread.h> |
| 25 | #include <linux/wait.h> |
| 26 | #include <linux/sched.h> |
Ingo Molnar | ae7e81c | 2017-02-01 18:07:51 +0100 | [diff] [blame] | 27 | #include <uapi/linux/sched/types.h> |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 28 | #include <drm/drmP.h> |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 29 | #include <drm/gpu_scheduler.h> |
| 30 | #include <drm/spsc_queue.h> |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 31 | |
Chunming Zhou | 353da3c | 2015-09-07 16:06:53 +0800 | [diff] [blame] | 32 | #define CREATE_TRACE_POINTS |
Nayan Deshmukh | a70cdb9 | 2018-03-29 22:36:33 +0530 | [diff] [blame] | 33 | #include "gpu_scheduler_trace.h" |
Chunming Zhou | 353da3c | 2015-09-07 16:06:53 +0800 | [diff] [blame] | 34 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 35 | #define to_drm_sched_job(sched_job) \ |
| 36 | container_of((sched_job), struct drm_sched_job, queue_node) |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 37 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 38 | static bool drm_sched_entity_is_ready(struct drm_sched_entity *entity); |
| 39 | static void drm_sched_wakeup(struct drm_gpu_scheduler *sched); |
| 40 | static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb); |
Christian König | 8807900 | 2015-08-24 14:29:40 +0200 | [diff] [blame] | 41 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 42 | /* Initialize a given run queue struct */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 43 | static void drm_sched_rq_init(struct drm_sched_rq *rq) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 44 | { |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 45 | spin_lock_init(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 46 | INIT_LIST_HEAD(&rq->entities); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 47 | rq->current_entity = NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 48 | } |
| 49 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 50 | static void drm_sched_rq_add_entity(struct drm_sched_rq *rq, |
| 51 | struct drm_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 52 | { |
Chunming Zhou | e8deea2 | 2015-12-11 18:22:52 +0800 | [diff] [blame] | 53 | if (!list_empty(&entity->list)) |
| 54 | return; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 55 | spin_lock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 56 | list_add_tail(&entity->list, &rq->entities); |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 57 | spin_unlock(&rq->lock); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 60 | static void drm_sched_rq_remove_entity(struct drm_sched_rq *rq, |
| 61 | struct drm_sched_entity *entity) |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 62 | { |
Chunming Zhou | e8deea2 | 2015-12-11 18:22:52 +0800 | [diff] [blame] | 63 | if (list_empty(&entity->list)) |
| 64 | return; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 65 | spin_lock(&rq->lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 66 | list_del_init(&entity->list); |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 67 | if (rq->current_entity == entity) |
| 68 | rq->current_entity = NULL; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 69 | spin_unlock(&rq->lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 73 | * Select an entity which could provide a job to run |
| 74 | * |
| 75 | * @rq The run queue to check. |
| 76 | * |
| 77 | * Try to find a ready entity, returns NULL if none found. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 78 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 79 | static struct drm_sched_entity * |
| 80 | drm_sched_rq_select_entity(struct drm_sched_rq *rq) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 81 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 82 | struct drm_sched_entity *entity; |
Christian König | 4cd7f42c | 2015-08-05 18:18:52 +0200 | [diff] [blame] | 83 | |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 84 | spin_lock(&rq->lock); |
| 85 | |
| 86 | entity = rq->current_entity; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 87 | if (entity) { |
| 88 | list_for_each_entry_continue(entity, &rq->entities, list) { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 89 | if (drm_sched_entity_is_ready(entity)) { |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 90 | rq->current_entity = entity; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 91 | spin_unlock(&rq->lock); |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 92 | return entity; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 93 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 94 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 95 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 96 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 97 | list_for_each_entry(entity, &rq->entities, list) { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 98 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 99 | if (drm_sched_entity_is_ready(entity)) { |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 100 | rq->current_entity = entity; |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 101 | spin_unlock(&rq->lock); |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 102 | return entity; |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 103 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 104 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 105 | if (entity == rq->current_entity) |
| 106 | break; |
| 107 | } |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 108 | |
Christian König | 2b184d8 | 2015-08-18 14:41:25 +0200 | [diff] [blame] | 109 | spin_unlock(&rq->lock); |
| 110 | |
Christian König | 432a4ff | 2015-08-12 11:46:04 +0200 | [diff] [blame] | 111 | return NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 115 | * Init a context entity used by scheduler when submit to HW ring. |
| 116 | * |
| 117 | * @sched The pointer to the scheduler |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 118 | * @entity The pointer to a valid drm_sched_entity |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 119 | * @rq The run queue this entity belongs |
Jammy Zhou | 1333f72 | 2015-07-30 16:36:58 +0800 | [diff] [blame] | 120 | * @jobs The max number of jobs in the job queue |
Nayan Deshmukh | ced5443 | 2018-03-29 22:36:31 +0530 | [diff] [blame] | 121 | * @guilty atomic_t set to 1 when a job on this queue |
| 122 | * is found to be guilty causing a timeout |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 123 | * |
| 124 | * return 0 if succeed. negative error code on failure |
| 125 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 126 | int drm_sched_entity_init(struct drm_gpu_scheduler *sched, |
| 127 | struct drm_sched_entity *entity, |
| 128 | struct drm_sched_rq *rq, |
Monk Liu | b3eebe3 | 2017-10-23 12:23:29 +0800 | [diff] [blame] | 129 | uint32_t jobs, atomic_t *guilty) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 130 | { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 131 | if (!(sched && entity && rq)) |
| 132 | return -EINVAL; |
| 133 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 134 | memset(entity, 0, sizeof(struct drm_sched_entity)); |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 135 | INIT_LIST_HEAD(&entity->list); |
| 136 | entity->rq = rq; |
| 137 | entity->sched = sched; |
Monk Liu | b3eebe3 | 2017-10-23 12:23:29 +0800 | [diff] [blame] | 138 | entity->guilty = guilty; |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 139 | entity->fini_status = 0; |
| 140 | entity->last_scheduled = NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 141 | |
Andres Rodriguez | 9ebbaab | 2017-06-02 15:09:00 -0400 | [diff] [blame] | 142 | spin_lock_init(&entity->rq_lock); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 143 | spin_lock_init(&entity->queue_lock); |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 144 | spsc_queue_init(&entity->job_queue); |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 145 | |
Christian König | ce882e6 | 2015-08-19 15:00:55 +0200 | [diff] [blame] | 146 | atomic_set(&entity->fence_seq, 0); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 147 | entity->fence_context = dma_fence_context_alloc(2); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 148 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 149 | return 0; |
| 150 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 151 | EXPORT_SYMBOL(drm_sched_entity_init); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Query if entity is initialized |
| 155 | * |
| 156 | * @sched Pointer to scheduler instance |
| 157 | * @entity The pointer to a valid scheduler entity |
| 158 | * |
| 159 | * return true if entity is initialized, false otherwise |
| 160 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 161 | static bool drm_sched_entity_is_initialized(struct drm_gpu_scheduler *sched, |
| 162 | struct drm_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 163 | { |
Christian König | 0f75aee | 2015-09-07 18:07:14 +0200 | [diff] [blame] | 164 | return entity->sched == sched && |
| 165 | entity->rq != NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 166 | } |
| 167 | |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 168 | /** |
| 169 | * Check if entity is idle |
| 170 | * |
| 171 | * @entity The pointer to a valid scheduler entity |
| 172 | * |
| 173 | * Return true if entity don't has any unscheduled jobs. |
| 174 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 175 | static bool drm_sched_entity_is_idle(struct drm_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 176 | { |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 177 | rmb(); |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 178 | if (spsc_queue_peek(&entity->job_queue) == NULL) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 179 | return true; |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | /** |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 185 | * Check if entity is ready |
| 186 | * |
| 187 | * @entity The pointer to a valid scheduler entity |
| 188 | * |
| 189 | * Return true if entity could provide a job. |
| 190 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 191 | static bool drm_sched_entity_is_ready(struct drm_sched_entity *entity) |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 192 | { |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 193 | if (spsc_queue_peek(&entity->job_queue) == NULL) |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 194 | return false; |
| 195 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 196 | if (READ_ONCE(entity->dependency)) |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 197 | return false; |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 202 | static void drm_sched_entity_kill_jobs_cb(struct dma_fence *f, |
| 203 | struct dma_fence_cb *cb) |
| 204 | { |
| 205 | struct drm_sched_job *job = container_of(cb, struct drm_sched_job, |
| 206 | finish_cb); |
| 207 | drm_sched_fence_finished(job->s_fence); |
| 208 | WARN_ON(job->s_fence->parent); |
| 209 | dma_fence_put(&job->s_fence->finished); |
| 210 | job->sched->ops->free_job(job); |
| 211 | } |
| 212 | |
| 213 | |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 214 | /** |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 215 | * Destroy a context entity |
| 216 | * |
| 217 | * @sched Pointer to scheduler instance |
| 218 | * @entity The pointer to a valid scheduler entity |
| 219 | * |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 220 | * Splitting drm_sched_entity_fini() into two functions, The first one is does the waiting, |
| 221 | * removes the entity from the runqueue and returns an error when the process was killed. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 222 | */ |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 223 | void drm_sched_entity_do_release(struct drm_gpu_scheduler *sched, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 224 | struct drm_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 225 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 226 | if (!drm_sched_entity_is_initialized(sched, entity)) |
Christian König | 062c7fb | 2015-08-21 15:46:43 +0200 | [diff] [blame] | 227 | return; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 228 | /** |
| 229 | * The client will not queue more IBs during this fini, consume existing |
Christian König | f0694d3 | 2017-08-21 14:27:51 +0200 | [diff] [blame] | 230 | * queued IBs or discard them on SIGKILL |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 231 | */ |
Christian König | f0694d3 | 2017-08-21 14:27:51 +0200 | [diff] [blame] | 232 | if ((current->flags & PF_SIGNALED) && current->exit_code == SIGKILL) |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 233 | entity->fini_status = -ERESTARTSYS; |
Christian König | f0694d3 | 2017-08-21 14:27:51 +0200 | [diff] [blame] | 234 | else |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 235 | entity->fini_status = wait_event_killable(sched->job_scheduled, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 236 | drm_sched_entity_is_idle(entity)); |
| 237 | drm_sched_entity_set_rq(entity, NULL); |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 238 | } |
| 239 | EXPORT_SYMBOL(drm_sched_entity_do_release); |
| 240 | |
| 241 | /** |
| 242 | * Destroy a context entity |
| 243 | * |
| 244 | * @sched Pointer to scheduler instance |
| 245 | * @entity The pointer to a valid scheduler entity |
| 246 | * |
| 247 | * The second one then goes over the entity and signals all jobs with an error code. |
| 248 | */ |
| 249 | void drm_sched_entity_cleanup(struct drm_gpu_scheduler *sched, |
| 250 | struct drm_sched_entity *entity) |
| 251 | { |
| 252 | if (entity->fini_status) { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 253 | struct drm_sched_job *job; |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 254 | int r; |
Alex Deucher | c945012 | 2017-10-12 13:08:48 -0400 | [diff] [blame] | 255 | |
Christian König | f0694d3 | 2017-08-21 14:27:51 +0200 | [diff] [blame] | 256 | /* Park the kernel for a moment to make sure it isn't processing |
| 257 | * our enity. |
| 258 | */ |
| 259 | kthread_park(sched->thread); |
| 260 | kthread_unpark(sched->thread); |
Chunming Zhou | f4323bc | 2017-11-07 10:40:00 +0800 | [diff] [blame] | 261 | if (entity->dependency) { |
| 262 | dma_fence_remove_callback(entity->dependency, |
| 263 | &entity->cb); |
| 264 | dma_fence_put(entity->dependency); |
| 265 | entity->dependency = NULL; |
| 266 | } |
| 267 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 268 | while ((job = to_drm_sched_job(spsc_queue_pop(&entity->job_queue)))) { |
| 269 | struct drm_sched_fence *s_fence = job->s_fence; |
| 270 | drm_sched_fence_scheduled(s_fence); |
Nicolai Hähnle | 7986746 | 2017-09-28 11:57:32 +0200 | [diff] [blame] | 271 | dma_fence_set_error(&s_fence->finished, -ESRCH); |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 272 | r = dma_fence_add_callback(entity->last_scheduled, &job->finish_cb, |
| 273 | drm_sched_entity_kill_jobs_cb); |
| 274 | if (r == -ENOENT) |
| 275 | drm_sched_entity_kill_jobs_cb(NULL, &job->finish_cb); |
| 276 | else if (r) |
| 277 | DRM_ERROR("fence add callback failed (%d)\n", r); |
Nicolai Hähnle | 7986746 | 2017-09-28 11:57:32 +0200 | [diff] [blame] | 278 | } |
Christian König | f0694d3 | 2017-08-21 14:27:51 +0200 | [diff] [blame] | 279 | } |
Pixel Ding | a4b3996 | 2018-04-18 04:33:26 -0400 | [diff] [blame] | 280 | |
| 281 | dma_fence_put(entity->last_scheduled); |
| 282 | entity->last_scheduled = NULL; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 283 | } |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 284 | EXPORT_SYMBOL(drm_sched_entity_cleanup); |
| 285 | |
| 286 | void drm_sched_entity_fini(struct drm_gpu_scheduler *sched, |
| 287 | struct drm_sched_entity *entity) |
| 288 | { |
| 289 | drm_sched_entity_do_release(sched, entity); |
| 290 | drm_sched_entity_cleanup(sched, entity); |
| 291 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 292 | EXPORT_SYMBOL(drm_sched_entity_fini); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 293 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 294 | static void drm_sched_entity_wakeup(struct dma_fence *f, struct dma_fence_cb *cb) |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 295 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 296 | struct drm_sched_entity *entity = |
| 297 | container_of(cb, struct drm_sched_entity, cb); |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 298 | entity->dependency = NULL; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 299 | dma_fence_put(f); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 300 | drm_sched_wakeup(entity->sched); |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 303 | static void drm_sched_entity_clear_dep(struct dma_fence *f, struct dma_fence_cb *cb) |
Monk Liu | 777dbd4 | 2016-01-26 14:59:57 +0800 | [diff] [blame] | 304 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 305 | struct drm_sched_entity *entity = |
| 306 | container_of(cb, struct drm_sched_entity, cb); |
Monk Liu | 777dbd4 | 2016-01-26 14:59:57 +0800 | [diff] [blame] | 307 | entity->dependency = NULL; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 308 | dma_fence_put(f); |
Monk Liu | 777dbd4 | 2016-01-26 14:59:57 +0800 | [diff] [blame] | 309 | } |
| 310 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 311 | void drm_sched_entity_set_rq(struct drm_sched_entity *entity, |
| 312 | struct drm_sched_rq *rq) |
Andres Rodriguez | 9ebbaab | 2017-06-02 15:09:00 -0400 | [diff] [blame] | 313 | { |
| 314 | if (entity->rq == rq) |
| 315 | return; |
| 316 | |
| 317 | spin_lock(&entity->rq_lock); |
| 318 | |
| 319 | if (entity->rq) |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 320 | drm_sched_rq_remove_entity(entity->rq, entity); |
Andres Rodriguez | 9ebbaab | 2017-06-02 15:09:00 -0400 | [diff] [blame] | 321 | |
| 322 | entity->rq = rq; |
| 323 | if (rq) |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 324 | drm_sched_rq_add_entity(rq, entity); |
Andres Rodriguez | 9ebbaab | 2017-06-02 15:09:00 -0400 | [diff] [blame] | 325 | |
| 326 | spin_unlock(&entity->rq_lock); |
| 327 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 328 | EXPORT_SYMBOL(drm_sched_entity_set_rq); |
Andres Rodriguez | 9ebbaab | 2017-06-02 15:09:00 -0400 | [diff] [blame] | 329 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 330 | bool drm_sched_dependency_optimized(struct dma_fence* fence, |
| 331 | struct drm_sched_entity *entity) |
Chunming Zhou | 30514de | 2017-05-09 13:39:40 +0800 | [diff] [blame] | 332 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 333 | struct drm_gpu_scheduler *sched = entity->sched; |
| 334 | struct drm_sched_fence *s_fence; |
Chunming Zhou | 30514de | 2017-05-09 13:39:40 +0800 | [diff] [blame] | 335 | |
| 336 | if (!fence || dma_fence_is_signaled(fence)) |
| 337 | return false; |
| 338 | if (fence->context == entity->fence_context) |
| 339 | return true; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 340 | s_fence = to_drm_sched_fence(fence); |
Chunming Zhou | 30514de | 2017-05-09 13:39:40 +0800 | [diff] [blame] | 341 | if (s_fence && s_fence->sched == sched) |
| 342 | return true; |
| 343 | |
| 344 | return false; |
| 345 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 346 | EXPORT_SYMBOL(drm_sched_dependency_optimized); |
Chunming Zhou | 30514de | 2017-05-09 13:39:40 +0800 | [diff] [blame] | 347 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 348 | static bool drm_sched_entity_add_dependency_cb(struct drm_sched_entity *entity) |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 349 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 350 | struct drm_gpu_scheduler *sched = entity->sched; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 351 | struct dma_fence * fence = entity->dependency; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 352 | struct drm_sched_fence *s_fence; |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 353 | |
| 354 | if (fence->context == entity->fence_context) { |
| 355 | /* We can ignore fences from ourself */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 356 | dma_fence_put(entity->dependency); |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 360 | s_fence = to_drm_sched_fence(fence); |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 361 | if (s_fence && s_fence->sched == sched) { |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 362 | |
Christian König | 6fc1367 | 2016-05-20 12:53:52 +0200 | [diff] [blame] | 363 | /* |
| 364 | * Fence is from the same scheduler, only need to wait for |
| 365 | * it to be scheduled |
| 366 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 367 | fence = dma_fence_get(&s_fence->scheduled); |
| 368 | dma_fence_put(entity->dependency); |
Christian König | 6fc1367 | 2016-05-20 12:53:52 +0200 | [diff] [blame] | 369 | entity->dependency = fence; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 370 | if (!dma_fence_add_callback(fence, &entity->cb, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 371 | drm_sched_entity_clear_dep)) |
Christian König | 6fc1367 | 2016-05-20 12:53:52 +0200 | [diff] [blame] | 372 | return true; |
| 373 | |
| 374 | /* Ignore it when it is already scheduled */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 375 | dma_fence_put(fence); |
Christian König | 6fc1367 | 2016-05-20 12:53:52 +0200 | [diff] [blame] | 376 | return false; |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 379 | if (!dma_fence_add_callback(entity->dependency, &entity->cb, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 380 | drm_sched_entity_wakeup)) |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 381 | return true; |
| 382 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 383 | dma_fence_put(entity->dependency); |
Christian König | 393a0bd | 2015-11-05 12:57:10 +0100 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 387 | static struct drm_sched_job * |
| 388 | drm_sched_entity_pop_job(struct drm_sched_entity *entity) |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 389 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 390 | struct drm_gpu_scheduler *sched = entity->sched; |
| 391 | struct drm_sched_job *sched_job = to_drm_sched_job( |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 392 | spsc_queue_peek(&entity->job_queue)); |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 393 | |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 394 | if (!sched_job) |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 395 | return NULL; |
| 396 | |
Andrey Grodzovsky | a4176cb | 2017-10-24 13:30:16 -0400 | [diff] [blame] | 397 | while ((entity->dependency = sched->ops->dependency(sched_job, entity))) |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 398 | if (drm_sched_entity_add_dependency_cb(entity)) |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 399 | return NULL; |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 400 | |
Monk Liu | 48f05f2 | 2017-10-25 16:21:08 +0800 | [diff] [blame] | 401 | /* skip jobs from entity that marked guilty */ |
| 402 | if (entity->guilty && atomic_read(entity->guilty)) |
| 403 | dma_fence_set_error(&sched_job->s_fence->finished, -ECANCELED); |
| 404 | |
Pixel Ding | b5b4ea4 | 2018-04-18 04:37:40 -0400 | [diff] [blame] | 405 | dma_fence_put(entity->last_scheduled); |
| 406 | entity->last_scheduled = dma_fence_get(&sched_job->s_fence->finished); |
| 407 | |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 408 | spsc_queue_pop(&entity->job_queue); |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 409 | return sched_job; |
Christian König | 69bd5bf | 2015-08-26 11:31:23 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 412 | /** |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 413 | * Submit a job to the job queue |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 414 | * |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 415 | * @sched_job The pointer to job required to submit |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 416 | * |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 417 | * Returns 0 for success, negative error code otherwise. |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 418 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 419 | void drm_sched_entity_push_job(struct drm_sched_job *sched_job, |
| 420 | struct drm_sched_entity *entity) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 421 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 422 | struct drm_gpu_scheduler *sched = sched_job->sched; |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 423 | bool first = false; |
| 424 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 425 | trace_drm_sched_job(sched_job, entity); |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 426 | |
| 427 | spin_lock(&entity->queue_lock); |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 428 | first = spsc_queue_push(&entity->job_queue, &sched_job->queue_node); |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 429 | |
| 430 | spin_unlock(&entity->queue_lock); |
| 431 | |
| 432 | /* first job wakes up scheduler */ |
Chunming Zhou | e8deea2 | 2015-12-11 18:22:52 +0800 | [diff] [blame] | 433 | if (first) { |
| 434 | /* Add the entity to the run queue */ |
Andres Rodriguez | 9ebbaab | 2017-06-02 15:09:00 -0400 | [diff] [blame] | 435 | spin_lock(&entity->rq_lock); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 436 | drm_sched_rq_add_entity(entity->rq, entity); |
Andres Rodriguez | 9ebbaab | 2017-06-02 15:09:00 -0400 | [diff] [blame] | 437 | spin_unlock(&entity->rq_lock); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 438 | drm_sched_wakeup(sched); |
Chunming Zhou | e8deea2 | 2015-12-11 18:22:52 +0800 | [diff] [blame] | 439 | } |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 440 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 441 | EXPORT_SYMBOL(drm_sched_entity_push_job); |
Christian König | 6c85927 | 2015-08-20 16:12:50 +0200 | [diff] [blame] | 442 | |
Nicolai Hähnle | 1650c14 | 2017-09-28 11:35:05 +0200 | [diff] [blame] | 443 | /* job_finish is called after hw fence signaled |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 444 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 445 | static void drm_sched_job_finish(struct work_struct *work) |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 446 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 447 | struct drm_sched_job *s_job = container_of(work, struct drm_sched_job, |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 448 | finish_work); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 449 | struct drm_gpu_scheduler *sched = s_job->sched; |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 450 | |
Christian König | f42d20a9 | 2016-05-18 15:40:58 +0200 | [diff] [blame] | 451 | /* remove job from ring_mirror_list */ |
Christian König | 1059e11 | 2016-06-13 16:12:43 +0200 | [diff] [blame] | 452 | spin_lock(&sched->job_list_lock); |
Christian König | f42d20a9 | 2016-05-18 15:40:58 +0200 | [diff] [blame] | 453 | list_del_init(&s_job->node); |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 454 | if (sched->timeout != MAX_SCHEDULE_TIMEOUT) { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 455 | struct drm_sched_job *next; |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 456 | |
Christian König | 1059e11 | 2016-06-13 16:12:43 +0200 | [diff] [blame] | 457 | spin_unlock(&sched->job_list_lock); |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 458 | cancel_delayed_work_sync(&s_job->work_tdr); |
Christian König | 1059e11 | 2016-06-13 16:12:43 +0200 | [diff] [blame] | 459 | spin_lock(&sched->job_list_lock); |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 460 | |
| 461 | /* queue TDR for next job */ |
| 462 | next = list_first_entry_or_null(&sched->ring_mirror_list, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 463 | struct drm_sched_job, node); |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 464 | |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 465 | if (next) |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 466 | schedule_delayed_work(&next->work_tdr, sched->timeout); |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 467 | } |
Christian König | 1059e11 | 2016-06-13 16:12:43 +0200 | [diff] [blame] | 468 | spin_unlock(&sched->job_list_lock); |
Christian König | 7fd5e36 | 2017-10-13 10:58:15 +0200 | [diff] [blame] | 469 | dma_fence_put(&s_job->s_fence->finished); |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 470 | sched->ops->free_job(s_job); |
| 471 | } |
| 472 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 473 | static void drm_sched_job_finish_cb(struct dma_fence *f, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 474 | struct dma_fence_cb *cb) |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 475 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 476 | struct drm_sched_job *job = container_of(cb, struct drm_sched_job, |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 477 | finish_cb); |
| 478 | schedule_work(&job->finish_work); |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 479 | } |
| 480 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 481 | static void drm_sched_job_begin(struct drm_sched_job *s_job) |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 482 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 483 | struct drm_gpu_scheduler *sched = s_job->sched; |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 484 | |
Nicolai Hähnle | 214a91e | 2017-09-28 11:37:02 +0200 | [diff] [blame] | 485 | dma_fence_add_callback(&s_job->s_fence->finished, &s_job->finish_cb, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 486 | drm_sched_job_finish_cb); |
Nicolai Hähnle | 214a91e | 2017-09-28 11:37:02 +0200 | [diff] [blame] | 487 | |
Christian König | 1059e11 | 2016-06-13 16:12:43 +0200 | [diff] [blame] | 488 | spin_lock(&sched->job_list_lock); |
Christian König | f42d20a9 | 2016-05-18 15:40:58 +0200 | [diff] [blame] | 489 | list_add_tail(&s_job->node, &sched->ring_mirror_list); |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 490 | if (sched->timeout != MAX_SCHEDULE_TIMEOUT && |
Christian König | 16a7133 | 2016-05-18 09:43:07 +0200 | [diff] [blame] | 491 | list_first_entry_or_null(&sched->ring_mirror_list, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 492 | struct drm_sched_job, node) == s_job) |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 493 | schedule_delayed_work(&s_job->work_tdr, sched->timeout); |
Christian König | 1059e11 | 2016-06-13 16:12:43 +0200 | [diff] [blame] | 494 | spin_unlock(&sched->job_list_lock); |
Monk Liu | 0de2479 | 2016-03-04 18:51:02 +0800 | [diff] [blame] | 495 | } |
| 496 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 497 | static void drm_sched_job_timedout(struct work_struct *work) |
Christian König | 0e51a77 | 2016-05-18 14:19:32 +0200 | [diff] [blame] | 498 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 499 | struct drm_sched_job *job = container_of(work, struct drm_sched_job, |
Christian König | 0e51a77 | 2016-05-18 14:19:32 +0200 | [diff] [blame] | 500 | work_tdr.work); |
| 501 | |
| 502 | job->sched->ops->timedout_job(job); |
| 503 | } |
| 504 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 505 | void drm_sched_hw_job_reset(struct drm_gpu_scheduler *sched, struct drm_sched_job *bad) |
Chunming Zhou | e686e75 | 2016-06-30 11:30:37 +0800 | [diff] [blame] | 506 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 507 | struct drm_sched_job *s_job; |
| 508 | struct drm_sched_entity *entity, *tmp; |
Luis de Bethencourt | e275149 | 2018-01-17 18:22:41 +0000 | [diff] [blame] | 509 | int i; |
Chunming Zhou | e686e75 | 2016-06-30 11:30:37 +0800 | [diff] [blame] | 510 | |
| 511 | spin_lock(&sched->job_list_lock); |
| 512 | list_for_each_entry_reverse(s_job, &sched->ring_mirror_list, node) { |
Chunming Zhou | a6bef67 | 2017-04-24 17:39:00 +0800 | [diff] [blame] | 513 | if (s_job->s_fence->parent && |
| 514 | dma_fence_remove_callback(s_job->s_fence->parent, |
| 515 | &s_job->s_fence->cb)) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 516 | dma_fence_put(s_job->s_fence->parent); |
Chunming Zhou | e686e75 | 2016-06-30 11:30:37 +0800 | [diff] [blame] | 517 | s_job->s_fence->parent = NULL; |
Monk Liu | 65781c7 | 2017-05-11 13:36:44 +0800 | [diff] [blame] | 518 | atomic_dec(&sched->hw_rq_count); |
Chunming Zhou | e686e75 | 2016-06-30 11:30:37 +0800 | [diff] [blame] | 519 | } |
| 520 | } |
Monk Liu | 65781c7 | 2017-05-11 13:36:44 +0800 | [diff] [blame] | 521 | spin_unlock(&sched->job_list_lock); |
Monk Liu | a8a51a7 | 2017-10-16 19:46:43 +0800 | [diff] [blame] | 522 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 523 | if (bad && bad->s_priority != DRM_SCHED_PRIORITY_KERNEL) { |
Monk Liu | cfb83b1 | 2017-11-08 14:35:04 +0800 | [diff] [blame] | 524 | atomic_inc(&bad->karma); |
Monk Liu | 48f05f2 | 2017-10-25 16:21:08 +0800 | [diff] [blame] | 525 | /* don't increase @bad's karma if it's from KERNEL RQ, |
| 526 | * becuase sometimes GPU hang would cause kernel jobs (like VM updating jobs) |
| 527 | * corrupt but keep in mind that kernel jobs always considered good. |
| 528 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 529 | for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_KERNEL; i++ ) { |
| 530 | struct drm_sched_rq *rq = &sched->sched_rq[i]; |
Monk Liu | a8a51a7 | 2017-10-16 19:46:43 +0800 | [diff] [blame] | 531 | |
| 532 | spin_lock(&rq->lock); |
| 533 | list_for_each_entry_safe(entity, tmp, &rq->entities, list) { |
| 534 | if (bad->s_fence->scheduled.context == entity->fence_context) { |
Monk Liu | cfb83b1 | 2017-11-08 14:35:04 +0800 | [diff] [blame] | 535 | if (atomic_read(&bad->karma) > bad->sched->hang_limit) |
Monk Liu | 48f05f2 | 2017-10-25 16:21:08 +0800 | [diff] [blame] | 536 | if (entity->guilty) |
| 537 | atomic_set(entity->guilty, 1); |
Monk Liu | a8a51a7 | 2017-10-16 19:46:43 +0800 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | } |
| 541 | spin_unlock(&rq->lock); |
Monk Liu | 48f05f2 | 2017-10-25 16:21:08 +0800 | [diff] [blame] | 542 | if (&entity->list != &rq->entities) |
Monk Liu | a8a51a7 | 2017-10-16 19:46:43 +0800 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | } |
Monk Liu | 65781c7 | 2017-05-11 13:36:44 +0800 | [diff] [blame] | 546 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 547 | EXPORT_SYMBOL(drm_sched_hw_job_reset); |
Monk Liu | 65781c7 | 2017-05-11 13:36:44 +0800 | [diff] [blame] | 548 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 549 | void drm_sched_job_recovery(struct drm_gpu_scheduler *sched) |
Monk Liu | 65781c7 | 2017-05-11 13:36:44 +0800 | [diff] [blame] | 550 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 551 | struct drm_sched_job *s_job, *tmp; |
Monk Liu | 48f05f2 | 2017-10-25 16:21:08 +0800 | [diff] [blame] | 552 | bool found_guilty = false; |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 553 | int r; |
| 554 | |
| 555 | spin_lock(&sched->job_list_lock); |
| 556 | s_job = list_first_entry_or_null(&sched->ring_mirror_list, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 557 | struct drm_sched_job, node); |
Christian König | bdf0013 | 2016-08-16 19:52:35 +0200 | [diff] [blame] | 558 | if (s_job && sched->timeout != MAX_SCHEDULE_TIMEOUT) |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 559 | schedule_delayed_work(&s_job->work_tdr, sched->timeout); |
| 560 | |
Chunming Zhou | 1c62cf9 | 2016-07-25 13:55:35 +0800 | [diff] [blame] | 561 | list_for_each_entry_safe(s_job, tmp, &sched->ring_mirror_list, node) { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 562 | struct drm_sched_fence *s_fence = s_job->s_fence; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 563 | struct dma_fence *fence; |
Monk Liu | 48f05f2 | 2017-10-25 16:21:08 +0800 | [diff] [blame] | 564 | uint64_t guilty_context; |
| 565 | |
| 566 | if (!found_guilty && atomic_read(&s_job->karma) > sched->hang_limit) { |
| 567 | found_guilty = true; |
| 568 | guilty_context = s_job->s_fence->scheduled.context; |
| 569 | } |
| 570 | |
| 571 | if (found_guilty && s_job->s_fence->scheduled.context == guilty_context) |
| 572 | dma_fence_set_error(&s_fence->finished, -ECANCELED); |
Chunming Zhou | bdc2eea | 2016-07-22 13:01:02 +0800 | [diff] [blame] | 573 | |
Chunming Zhou | 1c62cf9 | 2016-07-25 13:55:35 +0800 | [diff] [blame] | 574 | spin_unlock(&sched->job_list_lock); |
| 575 | fence = sched->ops->run_job(s_job); |
Chunming Zhou | bdc2eea | 2016-07-22 13:01:02 +0800 | [diff] [blame] | 576 | atomic_inc(&sched->hw_rq_count); |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 577 | |
| 578 | dma_fence_put(s_job->entity->last_scheduled); |
| 579 | s_job->entity->last_scheduled = dma_fence_get(&s_fence->finished); |
| 580 | |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 581 | if (fence) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 582 | s_fence->parent = dma_fence_get(fence); |
| 583 | r = dma_fence_add_callback(fence, &s_fence->cb, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 584 | drm_sched_process_job); |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 585 | if (r == -ENOENT) |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 586 | drm_sched_process_job(fence, &s_fence->cb); |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 587 | else if (r) |
| 588 | DRM_ERROR("fence add callback failed (%d)\n", |
| 589 | r); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 590 | dma_fence_put(fence); |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 591 | } else { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 592 | drm_sched_process_job(NULL, &s_fence->cb); |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 593 | } |
Chunming Zhou | 1c62cf9 | 2016-07-25 13:55:35 +0800 | [diff] [blame] | 594 | spin_lock(&sched->job_list_lock); |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 595 | } |
| 596 | spin_unlock(&sched->job_list_lock); |
| 597 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 598 | EXPORT_SYMBOL(drm_sched_job_recovery); |
Chunming Zhou | ec75f57 | 2016-06-29 15:23:55 +0800 | [diff] [blame] | 599 | |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 600 | /* init a sched_job with basic field */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 601 | int drm_sched_job_init(struct drm_sched_job *job, |
| 602 | struct drm_gpu_scheduler *sched, |
| 603 | struct drm_sched_entity *entity, |
Christian König | 595a9cd | 2016-06-30 10:52:03 +0200 | [diff] [blame] | 604 | void *owner) |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 605 | { |
| 606 | job->sched = sched; |
Emily Deng | 8ee3a52 | 2018-04-16 10:07:02 +0800 | [diff] [blame] | 607 | job->entity = entity; |
Andrey Grodzovsky | d1f6dc1 | 2017-10-19 14:29:46 -0400 | [diff] [blame] | 608 | job->s_priority = entity->rq - sched->sched_rq; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 609 | job->s_fence = drm_sched_fence_create(entity, owner); |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 610 | if (!job->s_fence) |
| 611 | return -ENOMEM; |
Chunming Zhou | cb3696f | 2017-05-09 15:34:07 +0800 | [diff] [blame] | 612 | job->id = atomic64_inc_return(&sched->job_id_count); |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 613 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 614 | INIT_WORK(&job->finish_work, drm_sched_job_finish); |
Christian König | c5f74f7 | 2016-05-19 09:54:15 +0200 | [diff] [blame] | 615 | INIT_LIST_HEAD(&job->node); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 616 | INIT_DELAYED_WORK(&job->work_tdr, drm_sched_job_timedout); |
Monk Liu | 4835096 | 2016-03-04 14:33:44 +0800 | [diff] [blame] | 617 | |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 618 | return 0; |
| 619 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 620 | EXPORT_SYMBOL(drm_sched_job_init); |
Monk Liu | e686941 | 2016-03-07 12:49:55 +0800 | [diff] [blame] | 621 | |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 622 | /** |
| 623 | * Return ture if we can push more jobs to the hw. |
| 624 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 625 | static bool drm_sched_ready(struct drm_gpu_scheduler *sched) |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 626 | { |
| 627 | return atomic_read(&sched->hw_rq_count) < |
| 628 | sched->hw_submission_limit; |
| 629 | } |
| 630 | |
| 631 | /** |
Christian König | 8807900 | 2015-08-24 14:29:40 +0200 | [diff] [blame] | 632 | * Wake up the scheduler when it is ready |
| 633 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 634 | static void drm_sched_wakeup(struct drm_gpu_scheduler *sched) |
Christian König | 8807900 | 2015-08-24 14:29:40 +0200 | [diff] [blame] | 635 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 636 | if (drm_sched_ready(sched)) |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 637 | wake_up_interruptible(&sched->wake_up_worker); |
Christian König | 8807900 | 2015-08-24 14:29:40 +0200 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 641 | * Select next entity to process |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 642 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 643 | static struct drm_sched_entity * |
| 644 | drm_sched_select_entity(struct drm_gpu_scheduler *sched) |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 645 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 646 | struct drm_sched_entity *entity; |
Chunming Zhou | d033a6d | 2015-11-05 15:23:09 +0800 | [diff] [blame] | 647 | int i; |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 648 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 649 | if (!drm_sched_ready(sched)) |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 650 | return NULL; |
| 651 | |
| 652 | /* Kernel run queue has higher priority than normal run queue*/ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 653 | for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) { |
| 654 | entity = drm_sched_rq_select_entity(&sched->sched_rq[i]); |
Chunming Zhou | d033a6d | 2015-11-05 15:23:09 +0800 | [diff] [blame] | 655 | if (entity) |
| 656 | break; |
| 657 | } |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 658 | |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 659 | return entity; |
Christian König | e688b728 | 2015-08-20 17:01:01 +0200 | [diff] [blame] | 660 | } |
| 661 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 662 | static void drm_sched_process_job(struct dma_fence *f, struct dma_fence_cb *cb) |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 663 | { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 664 | struct drm_sched_fence *s_fence = |
| 665 | container_of(cb, struct drm_sched_fence, cb); |
| 666 | struct drm_gpu_scheduler *sched = s_fence->sched; |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 667 | |
Christian König | 7fd5e36 | 2017-10-13 10:58:15 +0200 | [diff] [blame] | 668 | dma_fence_get(&s_fence->finished); |
Christian König | c746ba2 | 2015-08-19 16:12:15 +0200 | [diff] [blame] | 669 | atomic_dec(&sched->hw_rq_count); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 670 | drm_sched_fence_finished(s_fence); |
Monk Liu | cccd9bc | 2016-03-04 14:42:26 +0800 | [diff] [blame] | 671 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 672 | trace_drm_sched_process_job(s_fence); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 673 | dma_fence_put(&s_fence->finished); |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 674 | wake_up_interruptible(&sched->wake_up_worker); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 677 | static bool drm_sched_blocked(struct drm_gpu_scheduler *sched) |
Chunming Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 678 | { |
| 679 | if (kthread_should_park()) { |
| 680 | kthread_parkme(); |
| 681 | return true; |
| 682 | } |
| 683 | |
| 684 | return false; |
| 685 | } |
| 686 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 687 | static int drm_sched_main(void *param) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 688 | { |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 689 | struct sched_param sparam = {.sched_priority = 1}; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 690 | struct drm_gpu_scheduler *sched = (struct drm_gpu_scheduler *)param; |
Andrey Grodzovsky | 83f4b11 | 2017-10-12 16:46:26 -0400 | [diff] [blame] | 691 | int r; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 692 | |
| 693 | sched_setscheduler(current, SCHED_FIFO, &sparam); |
| 694 | |
| 695 | while (!kthread_should_stop()) { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 696 | struct drm_sched_entity *entity = NULL; |
| 697 | struct drm_sched_fence *s_fence; |
| 698 | struct drm_sched_job *sched_job; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 699 | struct dma_fence *fence; |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 700 | |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 701 | wait_event_interruptible(sched->wake_up_worker, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 702 | (!drm_sched_blocked(sched) && |
| 703 | (entity = drm_sched_select_entity(sched))) || |
Chunming Zhou | 0875dc9 | 2016-06-12 15:41:58 +0800 | [diff] [blame] | 704 | kthread_should_stop()); |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 705 | |
Christian König | 3d65193 | 2015-11-12 21:10:35 +0100 | [diff] [blame] | 706 | if (!entity) |
| 707 | continue; |
| 708 | |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 709 | sched_job = drm_sched_entity_pop_job(entity); |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 710 | if (!sched_job) |
Christian König | f85a6dd | 2015-08-19 17:37:52 +0200 | [diff] [blame] | 711 | continue; |
| 712 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 713 | s_fence = sched_job->s_fence; |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 714 | |
Christian König | b034b57 | 2015-08-20 17:08:25 +0200 | [diff] [blame] | 715 | atomic_inc(&sched->hw_rq_count); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 716 | drm_sched_job_begin(sched_job); |
Christian König | 7392c32 | 2016-05-18 13:00:38 +0200 | [diff] [blame] | 717 | |
Junwei Zhang | 4c7eb91 | 2015-09-09 09:05:55 +0800 | [diff] [blame] | 718 | fence = sched->ops->run_job(sched_job); |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 719 | drm_sched_fence_scheduled(s_fence); |
Nicolai Hähnle | 29d2535 | 2017-09-28 11:51:32 +0200 | [diff] [blame] | 720 | |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 721 | if (fence) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 722 | s_fence->parent = dma_fence_get(fence); |
| 723 | r = dma_fence_add_callback(fence, &s_fence->cb, |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 724 | drm_sched_process_job); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 725 | if (r == -ENOENT) |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 726 | drm_sched_process_job(fence, &s_fence->cb); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 727 | else if (r) |
Christian König | 16a7133 | 2016-05-18 09:43:07 +0200 | [diff] [blame] | 728 | DRM_ERROR("fence add callback failed (%d)\n", |
| 729 | r); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 730 | dma_fence_put(fence); |
Christian König | 27439fc | 2015-09-02 12:03:06 +0200 | [diff] [blame] | 731 | } else { |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 732 | drm_sched_process_job(NULL, &s_fence->cb); |
Christian König | 6f0e54a | 2015-08-05 21:22:10 +0200 | [diff] [blame] | 733 | } |
Christian König | aef4852 | 2015-08-20 14:47:46 +0200 | [diff] [blame] | 734 | |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 735 | wake_up(&sched->job_scheduled); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 736 | } |
| 737 | return 0; |
| 738 | } |
| 739 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 740 | /** |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 741 | * Init a gpu scheduler instance |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 742 | * |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 743 | * @sched The pointer to the scheduler |
Christian König | 69f7dd6 | 2015-08-20 17:24:40 +0200 | [diff] [blame] | 744 | * @ops The backend operations for this scheduler. |
Christian König | 69f7dd6 | 2015-08-20 17:24:40 +0200 | [diff] [blame] | 745 | * @hw_submissions Number of hw submissions to do. |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 746 | * @name Name used for debugging |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 747 | * |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 748 | * Return 0 on success, otherwise error code. |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 749 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 750 | int drm_sched_init(struct drm_gpu_scheduler *sched, |
| 751 | const struct drm_sched_backend_ops *ops, |
Monk Liu | 95aa9b1 | 2017-10-17 13:40:54 +0800 | [diff] [blame] | 752 | unsigned hw_submission, |
| 753 | unsigned hang_limit, |
| 754 | long timeout, |
| 755 | const char *name) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 756 | { |
Chunming Zhou | d033a6d | 2015-11-05 15:23:09 +0800 | [diff] [blame] | 757 | int i; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 758 | sched->ops = ops; |
Chunming Zhou | 4cef926 | 2015-08-05 19:52:14 +0800 | [diff] [blame] | 759 | sched->hw_submission_limit = hw_submission; |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 760 | sched->name = name; |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 761 | sched->timeout = timeout; |
Monk Liu | 95aa9b1 | 2017-10-17 13:40:54 +0800 | [diff] [blame] | 762 | sched->hang_limit = hang_limit; |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 763 | for (i = DRM_SCHED_PRIORITY_MIN; i < DRM_SCHED_PRIORITY_MAX; i++) |
| 764 | drm_sched_rq_init(&sched->sched_rq[i]); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 765 | |
Christian König | c2b6bd7 | 2015-08-25 21:39:31 +0200 | [diff] [blame] | 766 | init_waitqueue_head(&sched->wake_up_worker); |
| 767 | init_waitqueue_head(&sched->job_scheduled); |
Monk Liu | 4835096 | 2016-03-04 14:33:44 +0800 | [diff] [blame] | 768 | INIT_LIST_HEAD(&sched->ring_mirror_list); |
| 769 | spin_lock_init(&sched->job_list_lock); |
Christian König | c746ba2 | 2015-08-19 16:12:15 +0200 | [diff] [blame] | 770 | atomic_set(&sched->hw_rq_count, 0); |
Andres Rodriguez | 93f8b36 | 2017-03-09 21:25:50 -0500 | [diff] [blame] | 771 | atomic64_set(&sched->job_id_count, 0); |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 772 | |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 773 | /* Each scheduler will run on a seperate kernel thread */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 774 | sched->thread = kthread_run(drm_sched_main, sched, sched->name); |
Christian König | f495659 | 2015-08-20 16:59:38 +0200 | [diff] [blame] | 775 | if (IS_ERR(sched->thread)) { |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 776 | DRM_ERROR("Failed to create scheduler for %s.\n", name); |
| 777 | return PTR_ERR(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 778 | } |
| 779 | |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 780 | return 0; |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 781 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 782 | EXPORT_SYMBOL(drm_sched_init); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 783 | |
| 784 | /** |
| 785 | * Destroy a gpu scheduler |
| 786 | * |
| 787 | * @sched The pointer to the scheduler |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 788 | */ |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 789 | void drm_sched_fini(struct drm_gpu_scheduler *sched) |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 790 | { |
Dave Airlie | 32544d0 | 2015-11-03 11:10:03 -0500 | [diff] [blame] | 791 | if (sched->thread) |
| 792 | kthread_stop(sched->thread); |
Jammy Zhou | a72ce6f | 2015-05-22 18:55:07 +0800 | [diff] [blame] | 793 | } |
Lucas Stach | 1b1f42d | 2017-12-06 17:49:39 +0100 | [diff] [blame] | 794 | EXPORT_SYMBOL(drm_sched_fini); |