xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_sched.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: amdgpu_sched.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2015 Advanced Micro Devices, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  *
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: amdgpu_sched.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
28 
29 #include <linux/kthread.h>
30 #include <linux/wait.h>
31 #include <linux/sched.h>
32 #include <drm/drmP.h>
33 #include "amdgpu.h"
34 #include "amdgpu_trace.h"
35 
36 static struct fence *amdgpu_sched_dependency(struct amd_sched_job *sched_job)
37 {
38 	struct amdgpu_job *job = to_amdgpu_job(sched_job);
39 	return amdgpu_sync_get_fence(&job->ibs->sync);
40 }
41 
42 static struct fence *amdgpu_sched_run_job(struct amd_sched_job *sched_job)
43 {
44 	struct amdgpu_fence *fence = NULL;
45 	struct amdgpu_job *job;
46 	int r;
47 
48 	if (!sched_job) {
49 		DRM_ERROR("job is null\n");
50 		return NULL;
51 	}
52 	job = to_amdgpu_job(sched_job);
53 	trace_amdgpu_sched_run_job(job);
54 	r = amdgpu_ib_schedule(job->adev, job->num_ibs, job->ibs, job->owner);
55 	if (r) {
56 		DRM_ERROR("Error scheduling IBs (%d)\n", r);
57 		goto err;
58 	}
59 
60 	fence = job->ibs[job->num_ibs - 1].fence;
61 	fence_get(&fence->base);
62 
63 err:
64 	if (job->free_job)
65 		job->free_job(job);
66 
67 	kfree(job);
68 	return fence ? &fence->base : NULL;
69 }
70 
71 struct amd_sched_backend_ops amdgpu_sched_ops = {
72 	.dependency = amdgpu_sched_dependency,
73 	.run_job = amdgpu_sched_run_job,
74 };
75 
76 int amdgpu_sched_ib_submit_kernel_helper(struct amdgpu_device *adev,
77 					 struct amdgpu_ring *ring,
78 					 struct amdgpu_ib *ibs,
79 					 unsigned num_ibs,
80 					 int (*free_job)(struct amdgpu_job *),
81 					 void *owner,
82 					 struct fence **f)
83 {
84 	int r = 0;
85 	if (amdgpu_enable_scheduler) {
86 		struct amdgpu_job *job =
87 			kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
88 		if (!job)
89 			return -ENOMEM;
90 		job->base.sched = &ring->sched;
91 		job->base.s_entity = &adev->kernel_ctx.rings[ring->idx].entity;
92 		job->base.s_fence = amd_sched_fence_create(job->base.s_entity, owner);
93 		if (!job->base.s_fence) {
94 			kfree(job);
95 			return -ENOMEM;
96 		}
97 		*f = fence_get(&job->base.s_fence->base);
98 
99 		job->adev = adev;
100 		job->ibs = ibs;
101 		job->num_ibs = num_ibs;
102 		job->owner = owner;
103 		job->free_job = free_job;
104 		amd_sched_entity_push_job(&job->base);
105 	} else {
106 		r = amdgpu_ib_schedule(adev, num_ibs, ibs, owner);
107 		if (r)
108 			return r;
109 		*f = fence_get(&ibs[num_ibs - 1].fence->base);
110 	}
111 
112 	return 0;
113 }
114