xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_job.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2015 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  *
23*b843c749SSergey Zigachev  */
24*b843c749SSergey Zigachev #include <linux/kthread.h>
25*b843c749SSergey Zigachev #include <linux/wait.h>
26*b843c749SSergey Zigachev #include <linux/sched.h>
27*b843c749SSergey Zigachev #include <drm/drmP.h>
28*b843c749SSergey Zigachev #include "amdgpu.h"
29*b843c749SSergey Zigachev #include "amdgpu_trace.h"
30*b843c749SSergey Zigachev 
amdgpu_job_timedout(struct drm_sched_job * s_job)31*b843c749SSergey Zigachev static void amdgpu_job_timedout(struct drm_sched_job *s_job)
32*b843c749SSergey Zigachev {
33*b843c749SSergey Zigachev 	struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched);
34*b843c749SSergey Zigachev 	struct amdgpu_job *job = to_amdgpu_job(s_job);
35*b843c749SSergey Zigachev 
36*b843c749SSergey Zigachev 	DRM_ERROR("ring %s timeout, signaled seq=%u, emitted seq=%u\n",
37*b843c749SSergey Zigachev 		  job->base.sched->name, atomic_read(&ring->fence_drv.last_seq),
38*b843c749SSergey Zigachev 		  ring->fence_drv.sync_seq);
39*b843c749SSergey Zigachev 
40*b843c749SSergey Zigachev 	amdgpu_device_gpu_recover(ring->adev, job, false);
41*b843c749SSergey Zigachev }
42*b843c749SSergey Zigachev 
amdgpu_job_alloc(struct amdgpu_device * adev,unsigned num_ibs,struct amdgpu_job ** job,struct amdgpu_vm * vm)43*b843c749SSergey Zigachev int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs,
44*b843c749SSergey Zigachev 		     struct amdgpu_job **job, struct amdgpu_vm *vm)
45*b843c749SSergey Zigachev {
46*b843c749SSergey Zigachev 	size_t size = sizeof(struct amdgpu_job);
47*b843c749SSergey Zigachev 
48*b843c749SSergey Zigachev 	if (num_ibs == 0)
49*b843c749SSergey Zigachev 		return -EINVAL;
50*b843c749SSergey Zigachev 
51*b843c749SSergey Zigachev 	size += sizeof(struct amdgpu_ib) * num_ibs;
52*b843c749SSergey Zigachev 
53*b843c749SSergey Zigachev 	*job = kzalloc(size, GFP_KERNEL);
54*b843c749SSergey Zigachev 	if (!*job)
55*b843c749SSergey Zigachev 		return -ENOMEM;
56*b843c749SSergey Zigachev 
57*b843c749SSergey Zigachev 	/*
58*b843c749SSergey Zigachev 	 * Initialize the scheduler to at least some ring so that we always
59*b843c749SSergey Zigachev 	 * have a pointer to adev.
60*b843c749SSergey Zigachev 	 */
61*b843c749SSergey Zigachev 	(*job)->base.sched = &adev->rings[0]->sched;
62*b843c749SSergey Zigachev 	(*job)->vm = vm;
63*b843c749SSergey Zigachev 	(*job)->ibs = (void *)&(*job)[1];
64*b843c749SSergey Zigachev 	(*job)->num_ibs = num_ibs;
65*b843c749SSergey Zigachev 
66*b843c749SSergey Zigachev 	amdgpu_sync_create(&(*job)->sync);
67*b843c749SSergey Zigachev 	amdgpu_sync_create(&(*job)->sched_sync);
68*b843c749SSergey Zigachev 	(*job)->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
69*b843c749SSergey Zigachev 	(*job)->vm_pd_addr = AMDGPU_BO_INVALID_OFFSET;
70*b843c749SSergey Zigachev 
71*b843c749SSergey Zigachev 	return 0;
72*b843c749SSergey Zigachev }
73*b843c749SSergey Zigachev 
amdgpu_job_alloc_with_ib(struct amdgpu_device * adev,unsigned size,struct amdgpu_job ** job)74*b843c749SSergey Zigachev int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size,
75*b843c749SSergey Zigachev 			     struct amdgpu_job **job)
76*b843c749SSergey Zigachev {
77*b843c749SSergey Zigachev 	int r;
78*b843c749SSergey Zigachev 
79*b843c749SSergey Zigachev 	r = amdgpu_job_alloc(adev, 1, job, NULL);
80*b843c749SSergey Zigachev 	if (r)
81*b843c749SSergey Zigachev 		return r;
82*b843c749SSergey Zigachev 
83*b843c749SSergey Zigachev 	r = amdgpu_ib_get(adev, NULL, size, &(*job)->ibs[0]);
84*b843c749SSergey Zigachev 	if (r)
85*b843c749SSergey Zigachev 		kfree(*job);
86*b843c749SSergey Zigachev 	else
87*b843c749SSergey Zigachev 		(*job)->vm_pd_addr = adev->gart.table_addr;
88*b843c749SSergey Zigachev 
89*b843c749SSergey Zigachev 	return r;
90*b843c749SSergey Zigachev }
91*b843c749SSergey Zigachev 
amdgpu_job_free_resources(struct amdgpu_job * job)92*b843c749SSergey Zigachev void amdgpu_job_free_resources(struct amdgpu_job *job)
93*b843c749SSergey Zigachev {
94*b843c749SSergey Zigachev 	struct amdgpu_ring *ring = to_amdgpu_ring(job->base.sched);
95*b843c749SSergey Zigachev 	struct dma_fence *f;
96*b843c749SSergey Zigachev 	unsigned i;
97*b843c749SSergey Zigachev 
98*b843c749SSergey Zigachev 	/* use sched fence if available */
99*b843c749SSergey Zigachev 	f = job->base.s_fence ? &job->base.s_fence->finished : job->fence;
100*b843c749SSergey Zigachev 
101*b843c749SSergey Zigachev 	for (i = 0; i < job->num_ibs; ++i)
102*b843c749SSergey Zigachev 		amdgpu_ib_free(ring->adev, &job->ibs[i], f);
103*b843c749SSergey Zigachev }
104*b843c749SSergey Zigachev 
amdgpu_job_free_cb(struct drm_sched_job * s_job)105*b843c749SSergey Zigachev static void amdgpu_job_free_cb(struct drm_sched_job *s_job)
106*b843c749SSergey Zigachev {
107*b843c749SSergey Zigachev 	struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched);
108*b843c749SSergey Zigachev 	struct amdgpu_job *job = to_amdgpu_job(s_job);
109*b843c749SSergey Zigachev 
110*b843c749SSergey Zigachev 	amdgpu_ring_priority_put(ring, s_job->s_priority);
111*b843c749SSergey Zigachev 	dma_fence_put(job->fence);
112*b843c749SSergey Zigachev 	amdgpu_sync_free(&job->sync);
113*b843c749SSergey Zigachev 	amdgpu_sync_free(&job->sched_sync);
114*b843c749SSergey Zigachev 	kfree(job);
115*b843c749SSergey Zigachev }
116*b843c749SSergey Zigachev 
amdgpu_job_free(struct amdgpu_job * job)117*b843c749SSergey Zigachev void amdgpu_job_free(struct amdgpu_job *job)
118*b843c749SSergey Zigachev {
119*b843c749SSergey Zigachev 	amdgpu_job_free_resources(job);
120*b843c749SSergey Zigachev 
121*b843c749SSergey Zigachev 	dma_fence_put(job->fence);
122*b843c749SSergey Zigachev 	amdgpu_sync_free(&job->sync);
123*b843c749SSergey Zigachev 	amdgpu_sync_free(&job->sched_sync);
124*b843c749SSergey Zigachev 	kfree(job);
125*b843c749SSergey Zigachev }
126*b843c749SSergey Zigachev 
amdgpu_job_submit(struct amdgpu_job * job,struct drm_sched_entity * entity,void * owner,struct dma_fence ** f)127*b843c749SSergey Zigachev int amdgpu_job_submit(struct amdgpu_job *job, struct drm_sched_entity *entity,
128*b843c749SSergey Zigachev 		      void *owner, struct dma_fence **f)
129*b843c749SSergey Zigachev {
130*b843c749SSergey Zigachev 	enum drm_sched_priority priority;
131*b843c749SSergey Zigachev 	struct amdgpu_ring *ring;
132*b843c749SSergey Zigachev 	int r;
133*b843c749SSergey Zigachev 
134*b843c749SSergey Zigachev 	if (!f)
135*b843c749SSergey Zigachev 		return -EINVAL;
136*b843c749SSergey Zigachev 
137*b843c749SSergey Zigachev 	r = drm_sched_job_init(&job->base, entity, owner);
138*b843c749SSergey Zigachev 	if (r)
139*b843c749SSergey Zigachev 		return r;
140*b843c749SSergey Zigachev 
141*b843c749SSergey Zigachev 	job->owner = owner;
142*b843c749SSergey Zigachev 	*f = dma_fence_get(&job->base.s_fence->finished);
143*b843c749SSergey Zigachev 	amdgpu_job_free_resources(job);
144*b843c749SSergey Zigachev 	priority = job->base.s_priority;
145*b843c749SSergey Zigachev 	drm_sched_entity_push_job(&job->base, entity);
146*b843c749SSergey Zigachev 
147*b843c749SSergey Zigachev 	ring = to_amdgpu_ring(entity->rq->sched);
148*b843c749SSergey Zigachev 	amdgpu_ring_priority_get(ring, priority);
149*b843c749SSergey Zigachev 
150*b843c749SSergey Zigachev 	return 0;
151*b843c749SSergey Zigachev }
152*b843c749SSergey Zigachev 
amdgpu_job_submit_direct(struct amdgpu_job * job,struct amdgpu_ring * ring,struct dma_fence ** fence)153*b843c749SSergey Zigachev int amdgpu_job_submit_direct(struct amdgpu_job *job, struct amdgpu_ring *ring,
154*b843c749SSergey Zigachev 			     struct dma_fence **fence)
155*b843c749SSergey Zigachev {
156*b843c749SSergey Zigachev 	int r;
157*b843c749SSergey Zigachev 
158*b843c749SSergey Zigachev 	job->base.sched = &ring->sched;
159*b843c749SSergey Zigachev 	r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, NULL, fence);
160*b843c749SSergey Zigachev 	job->fence = dma_fence_get(*fence);
161*b843c749SSergey Zigachev 	if (r)
162*b843c749SSergey Zigachev 		return r;
163*b843c749SSergey Zigachev 
164*b843c749SSergey Zigachev 	amdgpu_job_free(job);
165*b843c749SSergey Zigachev 	return 0;
166*b843c749SSergey Zigachev }
167*b843c749SSergey Zigachev 
amdgpu_job_dependency(struct drm_sched_job * sched_job,struct drm_sched_entity * s_entity)168*b843c749SSergey Zigachev static struct dma_fence *amdgpu_job_dependency(struct drm_sched_job *sched_job,
169*b843c749SSergey Zigachev 					       struct drm_sched_entity *s_entity)
170*b843c749SSergey Zigachev {
171*b843c749SSergey Zigachev 	struct amdgpu_ring *ring = to_amdgpu_ring(s_entity->rq->sched);
172*b843c749SSergey Zigachev 	struct amdgpu_job *job = to_amdgpu_job(sched_job);
173*b843c749SSergey Zigachev 	struct amdgpu_vm *vm = job->vm;
174*b843c749SSergey Zigachev 	struct dma_fence *fence;
175*b843c749SSergey Zigachev 	bool explicit = false;
176*b843c749SSergey Zigachev 	int r;
177*b843c749SSergey Zigachev 
178*b843c749SSergey Zigachev 	fence = amdgpu_sync_get_fence(&job->sync, &explicit);
179*b843c749SSergey Zigachev 	if (fence && explicit) {
180*b843c749SSergey Zigachev 		if (drm_sched_dependency_optimized(fence, s_entity)) {
181*b843c749SSergey Zigachev 			r = amdgpu_sync_fence(ring->adev, &job->sched_sync,
182*b843c749SSergey Zigachev 					      fence, false);
183*b843c749SSergey Zigachev 			if (r)
184*b843c749SSergey Zigachev 				DRM_ERROR("Error adding fence (%d)\n", r);
185*b843c749SSergey Zigachev 		}
186*b843c749SSergey Zigachev 	}
187*b843c749SSergey Zigachev 
188*b843c749SSergey Zigachev 	while (fence == NULL && vm && !job->vmid) {
189*b843c749SSergey Zigachev 		r = amdgpu_vmid_grab(vm, ring, &job->sync,
190*b843c749SSergey Zigachev 				     &job->base.s_fence->finished,
191*b843c749SSergey Zigachev 				     job);
192*b843c749SSergey Zigachev 		if (r)
193*b843c749SSergey Zigachev 			DRM_ERROR("Error getting VM ID (%d)\n", r);
194*b843c749SSergey Zigachev 
195*b843c749SSergey Zigachev 		fence = amdgpu_sync_get_fence(&job->sync, NULL);
196*b843c749SSergey Zigachev 	}
197*b843c749SSergey Zigachev 
198*b843c749SSergey Zigachev 	return fence;
199*b843c749SSergey Zigachev }
200*b843c749SSergey Zigachev 
amdgpu_job_run(struct drm_sched_job * sched_job)201*b843c749SSergey Zigachev static struct dma_fence *amdgpu_job_run(struct drm_sched_job *sched_job)
202*b843c749SSergey Zigachev {
203*b843c749SSergey Zigachev 	struct amdgpu_ring *ring = to_amdgpu_ring(sched_job->sched);
204*b843c749SSergey Zigachev 	struct dma_fence *fence = NULL, *finished;
205*b843c749SSergey Zigachev 	struct amdgpu_job *job;
206*b843c749SSergey Zigachev 	int r = 0;
207*b843c749SSergey Zigachev 
208*b843c749SSergey Zigachev 	job = to_amdgpu_job(sched_job);
209*b843c749SSergey Zigachev 	finished = &job->base.s_fence->finished;
210*b843c749SSergey Zigachev 
211*b843c749SSergey Zigachev 	BUG_ON(amdgpu_sync_peek_fence(&job->sync, NULL));
212*b843c749SSergey Zigachev 
213*b843c749SSergey Zigachev 	trace_amdgpu_sched_run_job(job);
214*b843c749SSergey Zigachev 
215*b843c749SSergey Zigachev 	if (job->vram_lost_counter != atomic_read(&ring->adev->vram_lost_counter))
216*b843c749SSergey Zigachev 		dma_fence_set_error(finished, -ECANCELED);/* skip IB as well if VRAM lost */
217*b843c749SSergey Zigachev 
218*b843c749SSergey Zigachev 	if (finished->error < 0) {
219*b843c749SSergey Zigachev 		DRM_INFO("Skip scheduling IBs!\n");
220*b843c749SSergey Zigachev 	} else {
221*b843c749SSergey Zigachev 		r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, job,
222*b843c749SSergey Zigachev 				       &fence);
223*b843c749SSergey Zigachev 		if (r)
224*b843c749SSergey Zigachev 			DRM_ERROR("Error scheduling IBs (%d)\n", r);
225*b843c749SSergey Zigachev 	}
226*b843c749SSergey Zigachev 	/* if gpu reset, hw fence will be replaced here */
227*b843c749SSergey Zigachev 	dma_fence_put(job->fence);
228*b843c749SSergey Zigachev 	job->fence = dma_fence_get(fence);
229*b843c749SSergey Zigachev 
230*b843c749SSergey Zigachev 	amdgpu_job_free_resources(job);
231*b843c749SSergey Zigachev 
232*b843c749SSergey Zigachev 	fence = r ? ERR_PTR(r) : fence;
233*b843c749SSergey Zigachev 	return fence;
234*b843c749SSergey Zigachev }
235*b843c749SSergey Zigachev 
236*b843c749SSergey Zigachev const struct drm_sched_backend_ops amdgpu_sched_ops = {
237*b843c749SSergey Zigachev 	.dependency = amdgpu_job_dependency,
238*b843c749SSergey Zigachev 	.run_job = amdgpu_job_run,
239*b843c749SSergey Zigachev 	.timedout_job = amdgpu_job_timedout,
240*b843c749SSergey Zigachev 	.free_job = amdgpu_job_free_cb
241*b843c749SSergey Zigachev };
242