amdgpu_job.c 5.6 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)->dep_sync);
67
	amdgpu_sync_create(&(*job)->sched_sync);
68

69 70 71
	return 0;
}

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

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

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

	return r;
}

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

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

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

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

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

111 112 113
void amdgpu_job_free(struct amdgpu_job *job)
{
	amdgpu_job_free_resources(job);
114

115
	dma_fence_put(job->fence);
116
	amdgpu_sync_free(&job->sync);
117
	amdgpu_sync_free(&job->dep_sync);
118
	amdgpu_sync_free(&job->sched_sync);
119 120 121
	kfree(job);
}

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

129 130 131
	if (!f)
		return -EINVAL;

132
	r = amd_sched_job_init(&job->base, &ring->sched, entity, owner);
133 134
	if (r)
		return r;
135 136

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

	return 0;
143 144
}

145
static struct dma_fence *amdgpu_job_dependency(struct amd_sched_job *sched_job)
146
{
147
	struct amdgpu_job *job = to_amdgpu_job(sched_job);
148
	struct amdgpu_vm *vm = job->vm;
149

150
	struct dma_fence *fence = amdgpu_sync_get_fence(&job->dep_sync);
151
	int r;
152

153 154 155 156 157 158 159
	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);
	}
	if (!fence)
		fence = amdgpu_sync_get_fence(&job->sync);
C
Chunming Zhou 已提交
160
	while (fence == NULL && vm && !job->vm_id) {
161
		struct amdgpu_ring *ring = job->ring;
162

163
		r = amdgpu_vm_grab_id(vm, ring, &job->sync,
164
				      &job->base.s_fence->finished,
165
				      job);
166
		if (r)
167 168
			DRM_ERROR("Error getting VM ID (%d)\n", r);

169
		fence = amdgpu_sync_get_fence(&job->sync);
170 171 172
	}

	return fence;
173 174
}

175
static struct dma_fence *amdgpu_job_run(struct amd_sched_job *sched_job)
176
{
177
	struct dma_fence *fence = NULL;
178
	struct amdgpu_job *job;
179
	struct amdgpu_fpriv *fpriv = NULL;
180
	int r;
181

182
	if (!sched_job) {
183
		DRM_ERROR("job is null\n");
184
		return NULL;
185
	}
186
	job = to_amdgpu_job(sched_job);
187

188
	BUG_ON(amdgpu_sync_peek_fence(&job->sync, NULL));
189

190
	trace_amdgpu_sched_run_job(job);
191 192 193 194 195 196 197 198 199 200
	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);
	}
201
	/* if gpu reset, hw fence will be replaced here */
202 203
	dma_fence_put(job->fence);
	job->fence = dma_fence_get(fence);
204
	amdgpu_job_free_resources(job);
205
	return fence;
206 207
}

208
const struct amd_sched_backend_ops amdgpu_sched_ops = {
209 210
	.dependency = amdgpu_job_dependency,
	.run_job = amdgpu_job_run,
211
	.timedout_job = amdgpu_job_timedout,
212
	.free_job = amdgpu_job_free_cb
213
};