amdgpu_job.c 5.8 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
	amdgpu_gpu_recover(job->adev, job);
41 42
}

43
int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs,
44
		     struct amdgpu_job **job, struct amdgpu_vm *vm)
45 46 47 48 49 50 51 52 53 54 55 56 57
{
	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;
58
	(*job)->vm = vm;
59 60 61
	(*job)->ibs = (void *)&(*job)[1];
	(*job)->num_ibs = num_ibs;

62
	amdgpu_sync_create(&(*job)->sync);
63
	amdgpu_sync_create(&(*job)->sched_sync);
64
	(*job)->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
65

66 67 68
	return 0;
}

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

74
	r = amdgpu_job_alloc(adev, 1, job, NULL);
75 76 77 78 79 80
	if (r)
		return r;

	r = amdgpu_ib_get(adev, NULL, size, &(*job)->ibs[0]);
	if (r)
		kfree(*job);
81 82
	else
		(*job)->vm_pd_addr = adev->gart.table_addr;
83 84 85 86

	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
	amdgpu_ring_priority_put(job->ring, s_job->s_priority);
104
	dma_fence_put(job->fence);
105
	amdgpu_sync_free(&job->sync);
106
	amdgpu_sync_free(&job->sched_sync);
M
Monk Liu 已提交
107 108 109
	kfree(job);
}

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

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

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

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

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

	job->owner = owner;
135
	job->fence_ctx = entity->fence_context;
136
	*f = dma_fence_get(&job->base.s_fence->finished);
137
	amdgpu_job_free_resources(job);
138
	amdgpu_ring_priority_get(job->ring, job->base.s_priority);
139
	amd_sched_entity_push_job(&job->base, entity);
140 141

	return 0;
142 143
}

144 145
static struct dma_fence *amdgpu_job_dependency(struct amd_sched_job *sched_job,
					       struct amd_sched_entity *s_entity)
146
{
147
	struct amdgpu_job *job = to_amdgpu_job(sched_job);
148
	struct amdgpu_vm *vm = job->vm;
149
	bool explicit = false;
150
	int r;
151 152 153 154 155 156 157 158
	struct dma_fence *fence = amdgpu_sync_get_fence(&job->sync, &explicit);

	if (fence && explicit) {
		if (amd_sched_dependency_optimized(fence, s_entity)) {
			r = amdgpu_sync_fence(job->adev, &job->sched_sync, fence, false);
			if (r)
				DRM_ERROR("Error adding fence to sync (%d)\n", r);
		}
159
	}
160

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

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

170
		fence = amdgpu_sync_get_fence(&job->sync, NULL);
171 172 173
	}

	return fence;
174 175
}

176
static struct dma_fence *amdgpu_job_run(struct amd_sched_job *sched_job)
177
{
178
	struct dma_fence *fence = NULL, *finished;
179
	struct amdgpu_device *adev;
180
	struct amdgpu_job *job;
181
	int r;
182

183
	if (!sched_job) {
184
		DRM_ERROR("job is null\n");
185
		return NULL;
186
	}
187
	job = to_amdgpu_job(sched_job);
188
	finished = &job->base.s_fence->finished;
189
	adev = job->adev;
190

191
	BUG_ON(amdgpu_sync_peek_fence(&job->sync, NULL));
192

193
	trace_amdgpu_sched_run_job(job);
194 195 196 197 198 199

	if (job->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
		dma_fence_set_error(finished, -ECANCELED);/* skip IB as well if VRAM lost */

	if (finished->error < 0) {
		DRM_INFO("Skip scheduling IBs!\n");
200 201 202
	} else {
		r = amdgpu_ib_schedule(job->ring, job->num_ibs, job->ibs, job,
				       &fence);
203 204 205
		if (r)
			DRM_ERROR("Error scheduling IBs (%d)\n", r);
	}
206
	/* if gpu reset, hw fence will be replaced here */
207 208
	dma_fence_put(job->fence);
	job->fence = dma_fence_get(fence);
209

210
	amdgpu_job_free_resources(job);
211
	return fence;
212 213
}

214
const struct amd_sched_backend_ops amdgpu_sched_ops = {
215 216
	.dependency = amdgpu_job_dependency,
	.run_job = amdgpu_job_run,
217
	.timedout_job = amdgpu_job_timedout,
218
	.free_job = amdgpu_job_free_cb
219
};