xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_sched.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
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 #include "amdgpu_sched.h"
33 #include "amdgpu_vm.h"
34 
35 static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
36 						  int fd,
37 						  int32_t priority)
38 {
39 	STUB();
40 	return -ENOSYS;
41 #ifdef notyet
42 	struct fd f = fdget(fd);
43 	struct amdgpu_fpriv *fpriv;
44 	struct amdgpu_ctx *ctx;
45 	uint32_t id;
46 	int r;
47 
48 	if (!f.file)
49 		return -EINVAL;
50 
51 	r = amdgpu_file_to_fpriv(f.file, &fpriv);
52 	if (r) {
53 		fdput(f);
54 		return r;
55 	}
56 
57 	idr_for_each_entry(&fpriv->ctx_mgr.ctx_handles, ctx, id)
58 		amdgpu_ctx_priority_override(ctx, priority);
59 
60 	fdput(f);
61 	return 0;
62 #endif
63 }
64 
65 static int amdgpu_sched_context_priority_override(struct amdgpu_device *adev,
66 						  int fd,
67 						  unsigned ctx_id,
68 						  int32_t priority)
69 {
70 	STUB();
71 	return -ENOSYS;
72 #ifdef notyet
73 	struct fd f = fdget(fd);
74 	struct amdgpu_fpriv *fpriv;
75 	struct amdgpu_ctx *ctx;
76 	int r;
77 
78 	if (!f.file)
79 		return -EINVAL;
80 
81 	r = amdgpu_file_to_fpriv(f.file, &fpriv);
82 	if (r) {
83 		fdput(f);
84 		return r;
85 	}
86 
87 	ctx = amdgpu_ctx_get(fpriv, ctx_id);
88 
89 	if (!ctx) {
90 		fdput(f);
91 		return -EINVAL;
92 	}
93 
94 	amdgpu_ctx_priority_override(ctx, priority);
95 	amdgpu_ctx_put(ctx);
96 	fdput(f);
97 
98 	return 0;
99 #endif
100 }
101 
102 int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
103 		       struct drm_file *filp)
104 {
105 	union drm_amdgpu_sched *args = data;
106 	struct amdgpu_device *adev = drm_to_adev(dev);
107 	int r;
108 
109 	/* First check the op, then the op's argument.
110 	 */
111 	switch (args->in.op) {
112 	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_OVERRIDE:
113 	case AMDGPU_SCHED_OP_CONTEXT_PRIORITY_OVERRIDE:
114 		break;
115 	default:
116 		DRM_ERROR("Invalid sched op specified: %d\n", args->in.op);
117 		return -EINVAL;
118 	}
119 
120 	if (!amdgpu_ctx_priority_is_valid(args->in.priority)) {
121 		WARN(1, "Invalid context priority %d\n", args->in.priority);
122 		return -EINVAL;
123 	}
124 
125 	switch (args->in.op) {
126 	case AMDGPU_SCHED_OP_PROCESS_PRIORITY_OVERRIDE:
127 		r = amdgpu_sched_process_priority_override(adev,
128 							   args->in.fd,
129 							   args->in.priority);
130 		break;
131 	case AMDGPU_SCHED_OP_CONTEXT_PRIORITY_OVERRIDE:
132 		r = amdgpu_sched_context_priority_override(adev,
133 							   args->in.fd,
134 							   args->in.ctx_id,
135 							   args->in.priority);
136 		break;
137 	default:
138 		/* Impossible.
139 		 */
140 		r = -EINVAL;
141 		break;
142 	}
143 
144 	return r;
145 }
146