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