xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_sched.c (revision 24bb5fcea3ed904bc467217bdaadb5dfc618d5bf)
1 /*
2  * Copyright 2017 Valve Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Andres Rodriguez <andresx7@gmail.com>
23  */
24 
25 #include <linux/fdtable.h>
26 #include <linux/file.h>
27 #include <linux/pid.h>
28 
29 #include <drm/amdgpu_drm.h>
30 
31 #include "amdgpu.h"
32 
33 #include "amdgpu_vm.h"
34 
35 int amdgpu_to_sched_priority(int amdgpu_priority,
36 			     enum drm_sched_priority *prio)
37 {
38 	switch (amdgpu_priority) {
39 	case AMDGPU_CTX_PRIORITY_VERY_HIGH:
40 		*prio = DRM_SCHED_PRIORITY_HIGH;
41 		break;
42 	case AMDGPU_CTX_PRIORITY_HIGH:
43 		*prio = DRM_SCHED_PRIORITY_HIGH;
44 		break;
45 	case AMDGPU_CTX_PRIORITY_NORMAL:
46 		*prio = DRM_SCHED_PRIORITY_NORMAL;
47 		break;
48 	case AMDGPU_CTX_PRIORITY_LOW:
49 	case AMDGPU_CTX_PRIORITY_VERY_LOW:
50 		*prio = DRM_SCHED_PRIORITY_MIN;
51 		break;
52 	case AMDGPU_CTX_PRIORITY_UNSET:
53 		*prio = DRM_SCHED_PRIORITY_UNSET;
54 		break;
55 	default:
56 		WARN(1, "Invalid context priority %d\n", amdgpu_priority);
57 		return -EINVAL;
58 	}
59 
60 	return 0;
61 }
62 
63 static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
64 						  int fd,
65 						  enum drm_sched_priority priority)
66 {
67 	STUB();
68 	return -ENOSYS;
69 #ifdef notyet
70 	struct fd f = fdget(fd);
71 	struct amdgpu_fpriv *fpriv;
72 	struct amdgpu_ctx *ctx;
73 	uint32_t id;
74 	int r;
75 
76 	if (!f.file)
77 		return -EINVAL;
78 
79 	r = amdgpu_file_to_fpriv(f.file, &fpriv);
80 	if (r) {
81 		fdput(f);
82 		return r;
83 	}
84 
85 	idr_for_each_entry(&fpriv->ctx_mgr.ctx_handles, ctx, id)
86 		amdgpu_ctx_priority_override(ctx, priority);
87 
88 	fdput(f);
89 	return 0;
90 #endif
91 }
92 
93 static int amdgpu_sched_context_priority_override(struct amdgpu_device *adev,
94 						  int fd,
95 						  unsigned ctx_id,
96 						  enum drm_sched_priority priority)
97 {
98 	STUB();
99 	return -ENOSYS;
100 #ifdef notyet
101 	struct fd f = fdget(fd);
102 	struct amdgpu_fpriv *fpriv;
103 	struct amdgpu_ctx *ctx;
104 	int r;
105 
106 	if (!f.file)
107 		return -EINVAL;
108 
109 	r = amdgpu_file_to_fpriv(f.file, &fpriv);
110 	if (r) {
111 		fdput(f);
112 		return r;
113 	}
114 
115 	ctx = amdgpu_ctx_get(fpriv, ctx_id);
116 
117 	if (!ctx) {
118 		fdput(f);
119 		return -EINVAL;
120 	}
121 
122 	amdgpu_ctx_priority_override(ctx, priority);
123 	amdgpu_ctx_put(ctx);
124 	fdput(f);
125 
126 	return 0;
127 #endif
128 }
129 
130 int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
131 		       struct drm_file *filp)
132 {
133 	union drm_amdgpu_sched *args = data;
134 	struct amdgpu_device *adev = drm_to_adev(dev);
135 	enum drm_sched_priority priority;
136 	int r;
137 
138 	/* First check the op, then the op's argument.
139 	 */
140 	switch (args->in.op) {
141 	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_OVERRIDE:
142 	case AMDGPU_SCHED_OP_CONTEXT_PRIORITY_OVERRIDE:
143 		break;
144 	default:
145 		DRM_ERROR("Invalid sched op specified: %d\n", args->in.op);
146 		return -EINVAL;
147 	}
148 
149 	r = amdgpu_to_sched_priority(args->in.priority, &priority);
150 	if (r)
151 		return r;
152 
153 	switch (args->in.op) {
154 	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_OVERRIDE:
155 		r = amdgpu_sched_process_priority_override(adev,
156 							   args->in.fd,
157 							   priority);
158 		break;
159 	case AMDGPU_SCHED_OP_CONTEXT_PRIORITY_OVERRIDE:
160 		r = amdgpu_sched_context_priority_override(adev,
161 							   args->in.fd,
162 							   args->in.ctx_id,
163 							   priority);
164 		break;
165 	default:
166 		/* Impossible.
167 		 */
168 		r = -EINVAL;
169 		break;
170 	}
171 
172 	return r;
173 }
174