GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_sched.c
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/pid.h>
27 #include <drm/amdgpu_drm.h>
28 #include "amdgpu.h"
29
30 #include "amdgpu_vm.h"
31
32 enum drm_sched_priority amdgpu_to_sched_priority(int amdgpu_priority)
33 {
34         switch (amdgpu_priority) {
35         case AMDGPU_CTX_PRIORITY_VERY_HIGH:
36                 return DRM_SCHED_PRIORITY_HIGH_HW;
37         case AMDGPU_CTX_PRIORITY_HIGH:
38                 return DRM_SCHED_PRIORITY_HIGH_SW;
39         case AMDGPU_CTX_PRIORITY_NORMAL:
40                 return DRM_SCHED_PRIORITY_NORMAL;
41         case AMDGPU_CTX_PRIORITY_LOW:
42         case AMDGPU_CTX_PRIORITY_VERY_LOW:
43                 return DRM_SCHED_PRIORITY_LOW;
44         case AMDGPU_CTX_PRIORITY_UNSET:
45                 return DRM_SCHED_PRIORITY_UNSET;
46         default:
47                 WARN(1, "Invalid context priority %d\n", amdgpu_priority);
48                 return DRM_SCHED_PRIORITY_INVALID;
49         }
50 }
51
52 static int amdgpu_sched_process_priority_override(struct amdgpu_device *adev,
53                                                   int fd,
54                                                   enum drm_sched_priority priority)
55 {
56         struct file *filp = fget(fd);
57         struct amdgpu_fpriv *fpriv;
58         struct amdgpu_ctx_mgr *mgr;
59         struct amdgpu_ctx *ctx;
60         uint32_t id;
61         int r;
62
63         if (!filp)
64                 return -EINVAL;
65
66         r = amdgpu_file_to_fpriv(filp, &fpriv);
67         if (r) {
68                 fput(filp);
69                 return r;
70         }
71
72         mgr = &fpriv->ctx_mgr;
73         mutex_lock(&mgr->lock);
74         idr_for_each_entry(&mgr->ctx_handles, ctx, id)
75                 amdgpu_ctx_priority_override(ctx, priority);
76         mutex_unlock(&mgr->lock);
77
78         fput(filp);
79
80         return 0;
81 }
82
83 static int amdgpu_sched_context_priority_override(struct amdgpu_device *adev,
84                                                   int fd,
85                                                   unsigned ctx_id,
86                                                   enum drm_sched_priority priority)
87 {
88         struct file *filp = fget(fd);
89         struct amdgpu_fpriv *fpriv;
90         struct amdgpu_ctx *ctx;
91         int r;
92
93         if (!filp)
94                 return -EINVAL;
95
96         r = amdgpu_file_to_fpriv(filp, &fpriv);
97         if (r) {
98                 fput(filp);
99                 return r;
100         }
101
102         ctx = amdgpu_ctx_get(fpriv, ctx_id);
103
104         if (!ctx) {
105                 fput(filp);
106                 return -EINVAL;
107         }
108
109         amdgpu_ctx_priority_override(ctx, priority);
110         amdgpu_ctx_put(ctx);
111         fput(filp);
112
113         return 0;
114 }
115
116 int amdgpu_sched_ioctl(struct drm_device *dev, void *data,
117                        struct drm_file *filp)
118 {
119         union drm_amdgpu_sched *args = data;
120         struct amdgpu_device *adev = dev->dev_private;
121         enum drm_sched_priority priority;
122         int r;
123
124         priority = amdgpu_to_sched_priority(args->in.priority);
125         if (priority == DRM_SCHED_PRIORITY_INVALID)
126                 return -EINVAL;
127
128         switch (args->in.op) {
129         case AMDGPU_SCHED_OP_PROCESS_PRIORITY_OVERRIDE:
130                 r = amdgpu_sched_process_priority_override(adev,
131                                                            args->in.fd,
132                                                            priority);
133                 break;
134         case AMDGPU_SCHED_OP_CONTEXT_PRIORITY_OVERRIDE:
135                 r = amdgpu_sched_context_priority_override(adev,
136                                                            args->in.fd,
137                                                            args->in.ctx_id,
138                                                            priority);
139                 break;
140         default:
141                 DRM_ERROR("Invalid sched op specified: %d\n", args->in.op);
142                 r = -EINVAL;
143                 break;
144         }
145
146         return r;
147 }