drm/amdgpu: fix and cleanup amd_sched_entity_push_job
[deliverable/linux.git] / drivers / gpu / drm / amd / scheduler / gpu_scheduler.c
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 "gpu_scheduler.h"
29
30 /* Initialize a given run queue struct */
31 static void amd_sched_rq_init(struct amd_sched_rq *rq)
32 {
33 spin_lock_init(&rq->lock);
34 INIT_LIST_HEAD(&rq->entities);
35 rq->current_entity = NULL;
36 }
37
38 static void amd_sched_rq_add_entity(struct amd_sched_rq *rq,
39 struct amd_sched_entity *entity)
40 {
41 spin_lock(&rq->lock);
42 list_add_tail(&entity->list, &rq->entities);
43 spin_unlock(&rq->lock);
44 }
45
46 static void amd_sched_rq_remove_entity(struct amd_sched_rq *rq,
47 struct amd_sched_entity *entity)
48 {
49 spin_lock(&rq->lock);
50 list_del_init(&entity->list);
51 if (rq->current_entity == entity)
52 rq->current_entity = NULL;
53 spin_unlock(&rq->lock);
54 }
55
56 /**
57 * Select next entity from a specified run queue with round robin policy.
58 * It could return the same entity as current one if current is the only
59 * available one in the queue. Return NULL if nothing available.
60 */
61 static struct amd_sched_entity *
62 amd_sched_rq_select_entity(struct amd_sched_rq *rq)
63 {
64 struct amd_sched_entity *entity;
65
66 spin_lock(&rq->lock);
67
68 entity = rq->current_entity;
69 if (entity) {
70 list_for_each_entry_continue(entity, &rq->entities, list) {
71 if (!kfifo_is_empty(&entity->job_queue)) {
72 rq->current_entity = entity;
73 spin_unlock(&rq->lock);
74 return rq->current_entity;
75 }
76 }
77 }
78
79 list_for_each_entry(entity, &rq->entities, list) {
80
81 if (!kfifo_is_empty(&entity->job_queue)) {
82 rq->current_entity = entity;
83 spin_unlock(&rq->lock);
84 return rq->current_entity;
85 }
86
87 if (entity == rq->current_entity)
88 break;
89 }
90
91 spin_unlock(&rq->lock);
92
93 return NULL;
94 }
95
96 /**
97 * Init a context entity used by scheduler when submit to HW ring.
98 *
99 * @sched The pointer to the scheduler
100 * @entity The pointer to a valid amd_sched_entity
101 * @rq The run queue this entity belongs
102 * @kernel If this is an entity for the kernel
103 * @jobs The max number of jobs in the job queue
104 *
105 * return 0 if succeed. negative error code on failure
106 */
107 int amd_sched_entity_init(struct amd_gpu_scheduler *sched,
108 struct amd_sched_entity *entity,
109 struct amd_sched_rq *rq,
110 uint32_t jobs)
111 {
112 char name[20];
113
114 if (!(sched && entity && rq))
115 return -EINVAL;
116
117 memset(entity, 0, sizeof(struct amd_sched_entity));
118 entity->belongto_rq = rq;
119 entity->scheduler = sched;
120 init_waitqueue_head(&entity->wait_queue);
121 entity->fence_context = fence_context_alloc(1);
122 snprintf(name, sizeof(name), "c_entity[%llu]", entity->fence_context);
123 memcpy(entity->name, name, 20);
124 if(kfifo_alloc(&entity->job_queue,
125 jobs * sizeof(void *),
126 GFP_KERNEL))
127 return -EINVAL;
128
129 spin_lock_init(&entity->queue_lock);
130 atomic_set(&entity->fence_seq, 0);
131
132 /* Add the entity to the run queue */
133 amd_sched_rq_add_entity(rq, entity);
134 return 0;
135 }
136
137 /**
138 * Query if entity is initialized
139 *
140 * @sched Pointer to scheduler instance
141 * @entity The pointer to a valid scheduler entity
142 *
143 * return true if entity is initialized, false otherwise
144 */
145 static bool amd_sched_entity_is_initialized(struct amd_gpu_scheduler *sched,
146 struct amd_sched_entity *entity)
147 {
148 return entity->scheduler == sched &&
149 entity->belongto_rq != NULL;
150 }
151
152 /**
153 * Check if entity is idle
154 *
155 * @entity The pointer to a valid scheduler entity
156 *
157 * Return true if entity don't has any unscheduled jobs.
158 */
159 static bool amd_sched_entity_is_idle(struct amd_sched_entity *entity)
160 {
161 rmb();
162 if (kfifo_is_empty(&entity->job_queue))
163 return true;
164
165 return false;
166 }
167
168 /**
169 * Destroy a context entity
170 *
171 * @sched Pointer to scheduler instance
172 * @entity The pointer to a valid scheduler entity
173 *
174 * return 0 if succeed. negative error code on failure
175 */
176 int amd_sched_entity_fini(struct amd_gpu_scheduler *sched,
177 struct amd_sched_entity *entity)
178 {
179 struct amd_sched_rq *rq = entity->belongto_rq;
180 long r;
181
182 if (!amd_sched_entity_is_initialized(sched, entity))
183 return 0;
184
185 /**
186 * The client will not queue more IBs during this fini, consume existing
187 * queued IBs
188 */
189 r = wait_event_timeout(entity->wait_queue,
190 amd_sched_entity_is_idle(entity),
191 msecs_to_jiffies(AMD_GPU_WAIT_IDLE_TIMEOUT_IN_MS));
192
193 if (r <= 0)
194 DRM_INFO("Entity %p is in waiting state during fini\n",
195 entity);
196
197 amd_sched_rq_remove_entity(rq, entity);
198 kfifo_free(&entity->job_queue);
199 return r;
200 }
201
202 /**
203 * Helper to submit a job to the job queue
204 *
205 * @job The pointer to job required to submit
206 *
207 * Returns true if we could submit the job.
208 */
209 static bool amd_sched_entity_in(struct amd_sched_job *job)
210 {
211 struct amd_sched_entity *entity = job->s_entity;
212 bool added, first = false;
213
214 spin_lock(&entity->queue_lock);
215 added = kfifo_in(&entity->job_queue, &job, sizeof(job)) == sizeof(job);
216
217 if (added && kfifo_len(&entity->job_queue) == sizeof(job))
218 first = true;
219
220 spin_unlock(&entity->queue_lock);
221
222 /* first job wakes up scheduler */
223 if (first)
224 wake_up_interruptible(&job->sched->wait_queue);
225
226 return added;
227 }
228
229 /**
230 * Submit a job to the job queue
231 *
232 * @job The pointer to job required to submit
233 *
234 * Returns 0 for success, negative error code otherwise.
235 */
236 int amd_sched_entity_push_job(struct amd_sched_job *sched_job)
237 {
238 struct amd_sched_entity *entity = sched_job->s_entity;
239 struct amd_sched_fence *fence = amd_sched_fence_create(entity);
240 int r;
241
242 if (!fence)
243 return -ENOMEM;
244
245 fence_get(&fence->base);
246 sched_job->s_fence = fence;
247
248 r = wait_event_interruptible(entity->wait_queue,
249 amd_sched_entity_in(sched_job));
250
251 return r;
252 }
253
254 /**
255 * Return ture if we can push more jobs to the hw.
256 */
257 static bool amd_sched_ready(struct amd_gpu_scheduler *sched)
258 {
259 return atomic_read(&sched->hw_rq_count) <
260 sched->hw_submission_limit;
261 }
262
263 /**
264 * Select next entity containing real IB submissions
265 */
266 static struct amd_sched_entity *
267 amd_sched_select_context(struct amd_gpu_scheduler *sched)
268 {
269 struct amd_sched_entity *tmp;
270
271 if (!amd_sched_ready(sched))
272 return NULL;
273
274 /* Kernel run queue has higher priority than normal run queue*/
275 tmp = amd_sched_rq_select_entity(&sched->kernel_rq);
276 if (tmp == NULL)
277 tmp = amd_sched_rq_select_entity(&sched->sched_rq);
278
279 return tmp;
280 }
281
282 static void amd_sched_process_job(struct fence *f, struct fence_cb *cb)
283 {
284 struct amd_sched_job *sched_job =
285 container_of(cb, struct amd_sched_job, cb);
286 struct amd_gpu_scheduler *sched;
287
288 sched = sched_job->sched;
289 amd_sched_fence_signal(sched_job->s_fence);
290 atomic_dec(&sched->hw_rq_count);
291 fence_put(&sched_job->s_fence->base);
292 sched->ops->process_job(sched, sched_job);
293 wake_up_interruptible(&sched->wait_queue);
294 }
295
296 static int amd_sched_main(void *param)
297 {
298 struct sched_param sparam = {.sched_priority = 1};
299 struct amd_gpu_scheduler *sched = (struct amd_gpu_scheduler *)param;
300 int r;
301
302 sched_setscheduler(current, SCHED_FIFO, &sparam);
303
304 while (!kthread_should_stop()) {
305 struct amd_sched_entity *c_entity = NULL;
306 struct amd_sched_job *job;
307 struct fence *fence;
308
309 wait_event_interruptible(sched->wait_queue,
310 kthread_should_stop() ||
311 (c_entity = amd_sched_select_context(sched)));
312
313 if (!c_entity)
314 continue;
315
316 r = kfifo_out(&c_entity->job_queue, &job, sizeof(void *));
317 if (r != sizeof(void *))
318 continue;
319 atomic_inc(&sched->hw_rq_count);
320
321 fence = sched->ops->run_job(sched, c_entity, job);
322 if (fence) {
323 r = fence_add_callback(fence, &job->cb,
324 amd_sched_process_job);
325 if (r == -ENOENT)
326 amd_sched_process_job(fence, &job->cb);
327 else if (r)
328 DRM_ERROR("fence add callback failed (%d)\n", r);
329 fence_put(fence);
330 }
331
332 wake_up(&c_entity->wait_queue);
333 }
334 return 0;
335 }
336
337 /**
338 * Create a gpu scheduler
339 *
340 * @ops The backend operations for this scheduler.
341 * @ring The the ring id for the scheduler.
342 * @hw_submissions Number of hw submissions to do.
343 *
344 * Return the pointer to scheduler for success, otherwise return NULL
345 */
346 struct amd_gpu_scheduler *amd_sched_create(struct amd_sched_backend_ops *ops,
347 unsigned ring, unsigned hw_submission)
348 {
349 struct amd_gpu_scheduler *sched;
350 char name[20];
351
352 sched = kzalloc(sizeof(struct amd_gpu_scheduler), GFP_KERNEL);
353 if (!sched)
354 return NULL;
355
356 sched->ops = ops;
357 sched->ring_id = ring;
358 sched->hw_submission_limit = hw_submission;
359 snprintf(name, sizeof(name), "gpu_sched[%d]", ring);
360 amd_sched_rq_init(&sched->sched_rq);
361 amd_sched_rq_init(&sched->kernel_rq);
362
363 init_waitqueue_head(&sched->wait_queue);
364 atomic_set(&sched->hw_rq_count, 0);
365 /* Each scheduler will run on a seperate kernel thread */
366 sched->thread = kthread_run(amd_sched_main, sched, name);
367 if (IS_ERR(sched->thread)) {
368 DRM_ERROR("Failed to create scheduler for id %d.\n", ring);
369 kfree(sched);
370 return NULL;
371 }
372
373 return sched;
374 }
375
376 /**
377 * Destroy a gpu scheduler
378 *
379 * @sched The pointer to the scheduler
380 *
381 * return 0 if succeed. -1 if failed.
382 */
383 int amd_sched_destroy(struct amd_gpu_scheduler *sched)
384 {
385 kthread_stop(sched->thread);
386 kfree(sched);
387 return 0;
388 }
This page took 0.03993 seconds and 6 git commands to generate.