blob: 9f2f19cc46251d068a44aeadc34380ef1721eb29 [file] [log] [blame]
Chunming Zhouc1b69ed2015-07-21 13:45:14 +08001/*
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"
29
30static int amdgpu_sched_prepare_job(struct amd_gpu_scheduler *sched,
31 struct amd_context_entity *c_entity,
32 void *job)
33{
34 int r = 0;
35 struct amdgpu_cs_parser *sched_job = (struct amdgpu_cs_parser *)job;
Jammy Zhoudd01d072015-07-30 17:19:52 +080036 if (sched_job->prepare_job) {
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080037 r = sched_job->prepare_job(sched_job);
Jammy Zhoudd01d072015-07-30 17:19:52 +080038 if (r) {
39 DRM_ERROR("Prepare job error\n");
40 schedule_work(&sched_job->job_work);
41 }
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080042 }
43 return r;
44}
45
Chunming Zhou74846672015-08-04 11:30:09 +080046static void amdgpu_fence_sched_cb(struct fence *f, struct fence_cb *cb)
47{
48 struct amdgpu_fence *fence =
49 container_of(cb, struct amdgpu_fence, cb);
50 amd_sched_isr(fence->ring->scheduler);
51}
52
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080053static void amdgpu_sched_run_job(struct amd_gpu_scheduler *sched,
54 struct amd_context_entity *c_entity,
55 void *job)
56{
57 int r = 0;
58 struct amdgpu_cs_parser *sched_job = (struct amdgpu_cs_parser *)job;
Chunming Zhou74846672015-08-04 11:30:09 +080059 struct amdgpu_fence *fence;
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080060
61 mutex_lock(&sched_job->job_lock);
62 r = amdgpu_ib_schedule(sched_job->adev,
63 sched_job->num_ibs,
64 sched_job->ibs,
65 sched_job->filp);
66 if (r)
67 goto err;
Chunming Zhou74846672015-08-04 11:30:09 +080068 fence = sched_job->ibs[sched_job->num_ibs - 1].fence;
69 if (fence_add_callback(&fence->base,
70 &fence->cb, amdgpu_fence_sched_cb))
71 goto err;
72
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080073 if (sched_job->run_job) {
74 r = sched_job->run_job(sched_job);
75 if (r)
76 goto err;
77 }
Jammy Zhouf95b7e32015-07-31 17:18:15 +080078
79 amd_sched_emit(c_entity, sched_job->ibs[sched_job->num_ibs - 1].sequence);
Chunming Zhou4b559c92015-07-21 15:53:04 +080080
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080081 mutex_unlock(&sched_job->job_lock);
82 return;
83err:
84 DRM_ERROR("Run job error\n");
85 mutex_unlock(&sched_job->job_lock);
86 schedule_work(&sched_job->job_work);
87}
88
89static void amdgpu_sched_process_job(struct amd_gpu_scheduler *sched, void *job)
90{
91 struct amdgpu_cs_parser *sched_job = NULL;
92 struct amdgpu_fence *fence = NULL;
93 struct amdgpu_ring *ring = NULL;
94 struct amdgpu_device *adev = NULL;
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080095
96 if (!job)
97 return;
98 sched_job = (struct amdgpu_cs_parser *)job;
99 fence = sched_job->ibs[sched_job->num_ibs - 1].fence;
100 if (!fence)
101 return;
102 ring = fence->ring;
103 adev = ring->adev;
104
Chunming Zhouc1b69ed2015-07-21 13:45:14 +0800105 schedule_work(&sched_job->job_work);
106}
107
108struct amd_sched_backend_ops amdgpu_sched_ops = {
109 .prepare_job = amdgpu_sched_prepare_job,
110 .run_job = amdgpu_sched_run_job,
111 .process_job = amdgpu_sched_process_job
112};
113
Chunming Zhou3c704e92015-07-29 10:33:14 +0800114int amdgpu_sched_ib_submit_kernel_helper(struct amdgpu_device *adev,
115 struct amdgpu_ring *ring,
116 struct amdgpu_ib *ibs,
117 unsigned num_ibs,
118 int (*free_job)(struct amdgpu_cs_parser *),
Chunming Zhou17635522015-08-03 11:43:19 +0800119 void *owner,
120 struct fence **f)
Chunming Zhou3c704e92015-07-29 10:33:14 +0800121{
122 int r = 0;
123 if (amdgpu_enable_scheduler) {
Chunming Zhou3c704e92015-07-29 10:33:14 +0800124 struct amdgpu_cs_parser *sched_job =
125 amdgpu_cs_parser_create(adev,
126 owner,
127 adev->kernel_ctx,
128 ibs, 1);
129 if(!sched_job) {
130 return -ENOMEM;
131 }
132 sched_job->free_job = free_job;
Jammy Zhouea199cc2015-07-31 16:47:28 +0800133 ibs[num_ibs - 1].sequence = amd_sched_push_job(ring->scheduler,
Chunming Zhou3c704e92015-07-29 10:33:14 +0800134 &adev->kernel_ctx->rings[ring->idx].c_entity,
135 sched_job);
136 r = amd_sched_wait_emit(
137 &adev->kernel_ctx->rings[ring->idx].c_entity,
Jammy Zhouea199cc2015-07-31 16:47:28 +0800138 ibs[num_ibs - 1].sequence, false, -1);
Chunming Zhou3c704e92015-07-29 10:33:14 +0800139 if (r)
140 WARN(true, "emit timeout\n");
141 } else
142 r = amdgpu_ib_schedule(adev, 1, ibs, owner);
Chunming Zhou17635522015-08-03 11:43:19 +0800143 if (r)
144 return r;
145 *f = &ibs[num_ibs - 1].fence->base;
146 return 0;
Chunming Zhou3c704e92015-07-29 10:33:14 +0800147}