blob: 84453c1c4b07336356edd45e32ffb7a8702ab5c5 [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"
Chunming Zhou7034dec2015-11-11 14:56:00 +080029#include "amdgpu_trace.h"
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080030
Christian König50838c82016-02-03 13:44:52 +010031int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs,
32 struct amdgpu_job **job)
33{
34 size_t size = sizeof(struct amdgpu_job);
35
36 if (num_ibs == 0)
37 return -EINVAL;
38
39 size += sizeof(struct amdgpu_ib) * num_ibs;
40
41 *job = kzalloc(size, GFP_KERNEL);
42 if (!*job)
43 return -ENOMEM;
44
45 (*job)->adev = adev;
46 (*job)->ibs = (void *)&(*job)[1];
47 (*job)->num_ibs = num_ibs;
48 (*job)->free_job = NULL;
49
50 return 0;
51}
52
53void amdgpu_job_free(struct amdgpu_job *job)
54{
55 unsigned i;
56
57 for (i = 0; i < job->num_ibs; ++i)
58 amdgpu_ib_free(job->adev, &job->ibs[i]);
59
60 amdgpu_bo_unref(&job->uf.bo);
61 /* TODO: Free the job structure here as well */
62}
63
Junwei Zhang4c7eb912015-09-09 09:05:55 +080064static struct fence *amdgpu_sched_dependency(struct amd_sched_job *sched_job)
Christian Könige61235d2015-08-25 11:05:36 +020065{
Junwei Zhanga6db8a32015-09-09 09:21:19 +080066 struct amdgpu_job *job = to_amdgpu_job(sched_job);
Christian König8d0a7ce2015-11-03 20:58:50 +010067 struct amdgpu_sync *sync = &job->ibs->sync;
68 struct amdgpu_vm *vm = job->ibs->vm;
69
70 struct fence *fence = amdgpu_sync_get_fence(sync);
71
72 if (fence == NULL && vm && !job->ibs->grabbed_vmid) {
Christian Königb07c60c2016-01-31 12:29:04 +010073 struct amdgpu_ring *ring = job->ring;
Christian König8d0a7ce2015-11-03 20:58:50 +010074 int r;
75
Christian König94dd0a42016-01-18 17:01:42 +010076 r = amdgpu_vm_grab_id(vm, ring, sync,
77 &job->base.s_fence->base);
78 if (r)
Christian König8d0a7ce2015-11-03 20:58:50 +010079 DRM_ERROR("Error getting VM ID (%d)\n", r);
Christian König94dd0a42016-01-18 17:01:42 +010080 else
Christian König8d0a7ce2015-11-03 20:58:50 +010081 job->ibs->grabbed_vmid = true;
Christian König8d0a7ce2015-11-03 20:58:50 +010082
83 fence = amdgpu_sync_get_fence(sync);
84 }
85
86 return fence;
Christian Könige61235d2015-08-25 11:05:36 +020087}
88
Junwei Zhang4c7eb912015-09-09 09:05:55 +080089static struct fence *amdgpu_sched_run_job(struct amd_sched_job *sched_job)
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080090{
Christian König1886d1a2015-08-31 17:28:28 +020091 struct amdgpu_fence *fence = NULL;
Junwei Zhang4c7eb912015-09-09 09:05:55 +080092 struct amdgpu_job *job;
Christian Königbd755d02015-08-24 14:57:26 +020093 int r;
Chunming Zhouc1b69ed2015-07-21 13:45:14 +080094
Junwei Zhang4c7eb912015-09-09 09:05:55 +080095 if (!sched_job) {
Chunming Zhou4cef9262015-08-05 19:52:14 +080096 DRM_ERROR("job is null\n");
Christian König6f0e54a2015-08-05 21:22:10 +020097 return NULL;
Chunming Zhou4cef9262015-08-05 19:52:14 +080098 }
Junwei Zhanga6db8a32015-09-09 09:21:19 +080099 job = to_amdgpu_job(sched_job);
Chunming Zhou7034dec2015-11-11 14:56:00 +0800100 trace_amdgpu_sched_run_job(job);
Christian Königb07c60c2016-01-31 12:29:04 +0100101 r = amdgpu_ib_schedule(job->ring, job->num_ibs, job->ibs, job->owner);
Christian König1886d1a2015-08-31 17:28:28 +0200102 if (r) {
103 DRM_ERROR("Error scheduling IBs (%d)\n", r);
Chunming Zhouc1b69ed2015-07-21 13:45:14 +0800104 goto err;
Christian König1886d1a2015-08-31 17:28:28 +0200105 }
106
Christian König6ef68c12015-10-22 15:16:22 +0200107 fence = job->ibs[job->num_ibs - 1].fence;
108 fence_get(&fence->base);
Chunming Zhou74846672015-08-04 11:30:09 +0800109
Christian König1886d1a2015-08-31 17:28:28 +0200110err:
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800111 if (job->free_job)
112 job->free_job(job);
Christian Königbf7ebae2015-08-18 15:30:26 +0200113
Junwei Zhang4c7eb912015-09-09 09:05:55 +0800114 kfree(job);
Christian König1886d1a2015-08-31 17:28:28 +0200115 return fence ? &fence->base : NULL;
Chunming Zhouc1b69ed2015-07-21 13:45:14 +0800116}
117
118struct amd_sched_backend_ops amdgpu_sched_ops = {
Christian Könige61235d2015-08-25 11:05:36 +0200119 .dependency = amdgpu_sched_dependency,
Chunming Zhouc1b69ed2015-07-21 13:45:14 +0800120 .run_job = amdgpu_sched_run_job,
Chunming Zhouc1b69ed2015-07-21 13:45:14 +0800121};
122
Chunming Zhou3c704e92015-07-29 10:33:14 +0800123int amdgpu_sched_ib_submit_kernel_helper(struct amdgpu_device *adev,
124 struct amdgpu_ring *ring,
125 struct amdgpu_ib *ibs,
126 unsigned num_ibs,
Chunming Zhoubb977d32015-08-18 15:16:40 +0800127 int (*free_job)(struct amdgpu_job *),
Chunming Zhou17635522015-08-03 11:43:19 +0800128 void *owner,
129 struct fence **f)
Chunming Zhou3c704e92015-07-29 10:33:14 +0800130{
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800131 struct amdgpu_job *job =
132 kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
133 if (!job)
134 return -ENOMEM;
135 job->base.sched = &ring->sched;
136 job->base.s_entity = &adev->kernel_ctx.rings[ring->idx].entity;
137 job->base.s_fence = amd_sched_fence_create(job->base.s_entity, owner);
138 if (!job->base.s_fence) {
139 kfree(job);
140 return -ENOMEM;
Chunming Zhouf556cb0c2015-08-02 11:18:04 +0800141 }
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800142 *f = fence_get(&job->base.s_fence->base);
143
144 job->adev = adev;
Christian Königb07c60c2016-01-31 12:29:04 +0100145 job->ring = ring;
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800146 job->ibs = ibs;
147 job->num_ibs = num_ibs;
148 job->owner = owner;
149 job->free_job = free_job;
150 amd_sched_entity_push_job(&job->base);
Chunming Zhou3c623382015-08-20 18:33:59 +0800151
Chunming Zhou17635522015-08-03 11:43:19 +0800152 return 0;
Chunming Zhou3c704e92015-07-29 10:33:14 +0800153}