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