amdgpu_job.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright 2015 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 *
 */
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/sched.h>
#include <drm/drmP.h>
#include "amdgpu.h"
29
#include "amdgpu_trace.h"
30

31
static void amdgpu_job_timedout(struct amd_sched_job *s_job)
32
{
33 34
	struct amdgpu_job *job = container_of(s_job, struct amdgpu_job, base);

35
	DRM_ERROR("ring %s timeout, last signaled seq=%u, last emitted seq=%u\n",
36 37 38
		  job->base.sched->name,
		  atomic_read(&job->ring->fence_drv.last_seq),
		  job->ring->fence_drv.sync_seq);
39 40

	if (amdgpu_sriov_vf(job->adev))
41
		amdgpu_sriov_gpu_reset(job->adev, job);
42 43
	else
		amdgpu_gpu_reset(job->adev);
44 45
}

46
int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs,
47
		     struct amdgpu_job **job, struct amdgpu_vm *vm)
48 49 50 51 52 53 54 55 56 57 58 59 60
{
	size_t size = sizeof(struct amdgpu_job);

	if (num_ibs == 0)
		return -EINVAL;

	size += sizeof(struct amdgpu_ib) * num_ibs;

	*job = kzalloc(size, GFP_KERNEL);
	if (!*job)
		return -ENOMEM;

	(*job)->adev = adev;
61
	(*job)->vm = vm;
62 63 64
	(*job)->ibs = (void *)&(*job)[1];
	(*job)->num_ibs = num_ibs;

65
	amdgpu_sync_create(&(*job)->sync);
66
	amdgpu_sync_create(&(*job)->sched_sync);
67

68 69 70
	return 0;
}

71 72 73 74 75
int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size,
			     struct amdgpu_job **job)
{
	int r;

76
	r = amdgpu_job_alloc(adev, 1, job, NULL);
77 78 79 80 81 82 83 84 85 86
	if (r)
		return r;

	r = amdgpu_ib_get(adev, NULL, size, &(*job)->ibs[0]);
	if (r)
		kfree(*job);

	return r;
}

87
void amdgpu_job_free_resources(struct amdgpu_job *job)
88
{
89
	struct dma_fence *f;
90 91
	unsigned i;

92
	/* use sched fence if available */
93
	f = job->base.s_fence ? &job->base.s_fence->finished : job->fence;
94 95

	for (i = 0; i < job->num_ibs; ++i)
96
		amdgpu_ib_free(job->adev, &job->ibs[i], f);
97 98
}

99
static void amdgpu_job_free_cb(struct amd_sched_job *s_job)
M
Monk Liu 已提交
100
{
101 102
	struct amdgpu_job *job = container_of(s_job, struct amdgpu_job, base);

103
	dma_fence_put(job->fence);
104
	amdgpu_sync_free(&job->sync);
105
	amdgpu_sync_free(&job->sched_sync);
M
Monk Liu 已提交
106 107 108
	kfree(job);
}

109 110 111
void amdgpu_job_free(struct amdgpu_job *job)
{
	amdgpu_job_free_resources(job);
112

113
	dma_fence_put(job->fence);
114
	amdgpu_sync_free(&job->sync);
115
	amdgpu_sync_free(&job->sched_sync);
116 117 118
	kfree(job);
}

119
int amdgpu_job_submit(struct amdgpu_job *job, struct amdgpu_ring *ring,
120
		      struct amd_sched_entity *entity, void *owner,
121
		      struct dma_fence **f)
122
{
123
	int r;
124 125
	job->ring = ring;

126 127 128
	if (!f)
		return -EINVAL;

129
	r = amd_sched_job_init(&job->base, &ring->sched, entity, owner);
130 131
	if (r)
		return r;
132 133

	job->owner = owner;
134
	job->fence_ctx = entity->fence_context;
135
	*f = dma_fence_get(&job->base.s_fence->finished);
136
	amdgpu_job_free_resources(job);
137 138 139
	amd_sched_entity_push_job(&job->base);

	return 0;
140 141
}

142
static struct dma_fence *amdgpu_job_dependency(struct amd_sched_job *sched_job)
143
{
144
	struct amdgpu_job *job = to_amdgpu_job(sched_job);
145
	struct amdgpu_vm *vm = job->vm;
146

147
	struct dma_fence *fence = amdgpu_sync_get_fence(&job->sync);
148
	int r;
149

C
Chunming Zhou 已提交
150
	while (fence == NULL && vm && !job->vm_id) {
151
		struct amdgpu_ring *ring = job->ring;
152

153
		r = amdgpu_vm_grab_id(vm, ring, &job->sync,
154
				      &job->base.s_fence->finished,
155
				      job);
156
		if (r)
157 158
			DRM_ERROR("Error getting VM ID (%d)\n", r);

159
		fence = amdgpu_sync_get_fence(&job->sync);
160 161
	}

162 163 164 165 166
	if (amd_sched_dependency_optimized(fence, sched_job->s_entity)) {
		r = amdgpu_sync_fence(job->adev, &job->sched_sync, fence);
		if (r)
			DRM_ERROR("Error adding fence to sync (%d)\n", r);
	}
167
	return fence;
168 169
}

170
static struct dma_fence *amdgpu_job_run(struct amd_sched_job *sched_job)
171
{
172
	struct dma_fence *fence = NULL;
173
	struct amdgpu_job *job;
174
	struct amdgpu_fpriv *fpriv = NULL;
175
	int r;
176

177
	if (!sched_job) {
178
		DRM_ERROR("job is null\n");
179
		return NULL;
180
	}
181
	job = to_amdgpu_job(sched_job);
182

183
	BUG_ON(amdgpu_sync_peek_fence(&job->sync, NULL));
184

185
	trace_amdgpu_sched_run_job(job);
186 187 188 189 190 191 192 193 194 195
	if (job->vm)
		fpriv = container_of(job->vm, struct amdgpu_fpriv, vm);
	/* skip ib schedule when vram is lost */
	if (fpriv && amdgpu_kms_vram_lost(job->adev, fpriv))
		DRM_ERROR("Skip scheduling IBs!\n");
	else {
		r = amdgpu_ib_schedule(job->ring, job->num_ibs, job->ibs, job, &fence);
		if (r)
			DRM_ERROR("Error scheduling IBs (%d)\n", r);
	}
196
	/* if gpu reset, hw fence will be replaced here */
197 198
	dma_fence_put(job->fence);
	job->fence = dma_fence_get(fence);
199
	amdgpu_job_free_resources(job);
200
	return fence;
201 202
}

203
const struct amd_sched_backend_ops amdgpu_sched_ops = {
204 205
	.dependency = amdgpu_job_dependency,
	.run_job = amdgpu_job_run,
206
	.timedout_job = amdgpu_job_timedout,
207
	.free_job = amdgpu_job_free_cb
208
};