amdgpu_job.c 6.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 drm_sched_job *s_job)
32
{
C
Christian König 已提交
33 34
	struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched);
	struct amdgpu_job *job = to_amdgpu_job(s_job);
35 36 37
	struct amdgpu_task_info ti;

	memset(&ti, 0, sizeof(struct amdgpu_task_info));
38

39 40 41 42 43 44
	if (amdgpu_ring_soft_recovery(ring, job->vmid, s_job->s_fence->parent)) {
		DRM_ERROR("ring %s timeout, but soft recovered\n",
			  s_job->sched->name);
		return;
	}

45
	amdgpu_vm_get_task_info(ring->adev, job->pasid, &ti);
46
	DRM_ERROR("ring %s timeout, signaled seq=%u, emitted seq=%u\n",
C
Christian König 已提交
47 48
		  job->base.sched->name, atomic_read(&ring->fence_drv.last_seq),
		  ring->fence_drv.sync_seq);
49 50
	DRM_ERROR("Process information: process %s pid %d thread %s pid %d\n",
		  ti.process_name, ti.tgid, ti.task_name, ti.pid);
51

52 53
	if (amdgpu_device_should_recover_gpu(ring->adev))
		amdgpu_device_gpu_recover(ring->adev, job);
54 55
}

56
int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs,
57
		     struct amdgpu_job **job, struct amdgpu_vm *vm)
58 59 60 61 62 63 64 65 66 67 68 69
{
	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;

70 71 72 73 74
	/*
	 * Initialize the scheduler to at least some ring so that we always
	 * have a pointer to adev.
	 */
	(*job)->base.sched = &adev->rings[0]->sched;
75
	(*job)->vm = vm;
76 77 78
	(*job)->ibs = (void *)&(*job)[1];
	(*job)->num_ibs = num_ibs;

79
	amdgpu_sync_create(&(*job)->sync);
80
	amdgpu_sync_create(&(*job)->sched_sync);
81
	(*job)->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
82
	(*job)->vm_pd_addr = AMDGPU_BO_INVALID_OFFSET;
83

84 85 86
	return 0;
}

87 88 89 90 91
int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size,
			     struct amdgpu_job **job)
{
	int r;

92
	r = amdgpu_job_alloc(adev, 1, job, NULL);
93 94 95 96 97 98 99 100 101 102
	if (r)
		return r;

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

	return r;
}

103
void amdgpu_job_free_resources(struct amdgpu_job *job)
104
{
105
	struct amdgpu_ring *ring = to_amdgpu_ring(job->base.sched);
106
	struct dma_fence *f;
107 108
	unsigned i;

109
	/* use sched fence if available */
110
	f = job->base.s_fence ? &job->base.s_fence->finished : job->fence;
111 112

	for (i = 0; i < job->num_ibs; ++i)
113
		amdgpu_ib_free(ring->adev, &job->ibs[i], f);
114 115
}

116
static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
M
Monk Liu 已提交
117
{
C
Christian König 已提交
118 119
	struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched);
	struct amdgpu_job *job = to_amdgpu_job(s_job);
120

121 122
	drm_sched_job_cleanup(s_job);

C
Christian König 已提交
123
	amdgpu_ring_priority_put(ring, s_job->s_priority);
124
	dma_fence_put(job->fence);
125
	amdgpu_sync_free(&job->sync);
126
	amdgpu_sync_free(&job->sched_sync);
M
Monk Liu 已提交
127 128 129
	kfree(job);
}

130 131 132
void amdgpu_job_free(struct amdgpu_job *job)
{
	amdgpu_job_free_resources(job);
133

134
	dma_fence_put(job->fence);
135
	amdgpu_sync_free(&job->sync);
136
	amdgpu_sync_free(&job->sched_sync);
137 138 139
	kfree(job);
}

140 141
int amdgpu_job_submit(struct amdgpu_job *job, struct drm_sched_entity *entity,
		      void *owner, struct dma_fence **f)
142
{
143 144
	enum drm_sched_priority priority;
	struct amdgpu_ring *ring;
145
	int r;
146

147 148 149
	if (!f)
		return -EINVAL;

150
	r = drm_sched_job_init(&job->base, entity, owner);
151 152
	if (r)
		return r;
153 154

	job->owner = owner;
155
	*f = dma_fence_get(&job->base.s_fence->finished);
156
	amdgpu_job_free_resources(job);
157
	priority = job->base.s_priority;
158
	drm_sched_entity_push_job(&job->base, entity);
159

160
	ring = to_amdgpu_ring(entity->rq->sched);
161 162
	amdgpu_ring_priority_get(ring, priority);

163
	return 0;
164 165
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
int amdgpu_job_submit_direct(struct amdgpu_job *job, struct amdgpu_ring *ring,
			     struct dma_fence **fence)
{
	int r;

	job->base.sched = &ring->sched;
	r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, NULL, fence);
	job->fence = dma_fence_get(*fence);
	if (r)
		return r;

	amdgpu_job_free(job);
	return 0;
}

181 182
static struct dma_fence *amdgpu_job_dependency(struct drm_sched_job *sched_job,
					       struct drm_sched_entity *s_entity)
183
{
184
	struct amdgpu_ring *ring = to_amdgpu_ring(s_entity->rq->sched);
185
	struct amdgpu_job *job = to_amdgpu_job(sched_job);
186
	struct amdgpu_vm *vm = job->vm;
187
	struct dma_fence *fence;
188
	bool explicit = false;
189
	int r;
190

191
	fence = amdgpu_sync_get_fence(&job->sync, &explicit);
192
	if (fence && explicit) {
193
		if (drm_sched_dependency_optimized(fence, s_entity)) {
194 195
			r = amdgpu_sync_fence(ring->adev, &job->sched_sync,
					      fence, false);
196
			if (r)
197
				DRM_ERROR("Error adding fence (%d)\n", r);
198
		}
199
	}
200

201
	while (fence == NULL && vm && !job->vmid) {
202 203 204
		r = amdgpu_vmid_grab(vm, ring, &job->sync,
				     &job->base.s_fence->finished,
				     job);
205
		if (r)
206 207
			DRM_ERROR("Error getting VM ID (%d)\n", r);

208
		fence = amdgpu_sync_get_fence(&job->sync, NULL);
209 210 211
	}

	return fence;
212 213
}

214
static struct dma_fence *amdgpu_job_run(struct drm_sched_job *sched_job)
215
{
C
Christian König 已提交
216
	struct amdgpu_ring *ring = to_amdgpu_ring(sched_job->sched);
217
	struct dma_fence *fence = NULL, *finished;
218
	struct amdgpu_job *job;
219
	int r;
220

221
	job = to_amdgpu_job(sched_job);
222
	finished = &job->base.s_fence->finished;
223

224
	BUG_ON(amdgpu_sync_peek_fence(&job->sync, NULL));
225

226
	trace_amdgpu_sched_run_job(job);
227

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

	if (finished->error < 0) {
		DRM_INFO("Skip scheduling IBs!\n");
233
	} else {
C
Christian König 已提交
234
		r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, job,
235
				       &fence);
236 237 238
		if (r)
			DRM_ERROR("Error scheduling IBs (%d)\n", r);
	}
239
	/* if gpu reset, hw fence will be replaced here */
240 241
	dma_fence_put(job->fence);
	job->fence = dma_fence_get(fence);
242

243
	amdgpu_job_free_resources(job);
244
	return fence;
245 246
}

247
const struct drm_sched_backend_ops amdgpu_sched_ops = {
248 249
	.dependency = amdgpu_job_dependency,
	.run_job = amdgpu_job_run,
250
	.timedout_job = amdgpu_job_timedout,
251
	.free_job = amdgpu_job_free_cb
252
};