1 /* 2 * Copyright 2015 Advanced Micro Devices, Inc. 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 */ 23 24 #include <linux/module.h> 25 #include <linux/sched.h> 26 #include <linux/slab.h> 27 #include <linux/wait.h> 28 29 #include <drm/gpu_scheduler.h> 30 31 #include "sched_internal.h" 32 33 static struct kmem_cache *sched_fence_slab; 34 35 static int __init drm_sched_fence_slab_init(void) 36 { 37 sched_fence_slab = KMEM_CACHE(drm_sched_fence, SLAB_HWCACHE_ALIGN); 38 if (!sched_fence_slab) 39 return -ENOMEM; 40 41 return 0; 42 } 43 44 static void __exit drm_sched_fence_slab_fini(void) 45 { 46 rcu_barrier(); 47 kmem_cache_destroy(sched_fence_slab); 48 } 49 50 static void drm_sched_fence_set_parent(struct drm_sched_fence *s_fence, 51 struct dma_fence *fence) 52 { 53 /* 54 * smp_store_release() to ensure another thread racing us 55 * in drm_sched_fence_set_deadline_finished() sees the 56 * fence's parent set before test_bit() 57 */ 58 smp_store_release(&s_fence->parent, dma_fence_get(fence)); 59 if (test_bit(DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT, 60 &s_fence->finished.flags)) 61 dma_fence_set_deadline(fence, s_fence->deadline); 62 } 63 64 void drm_sched_fence_scheduled(struct drm_sched_fence *fence, 65 struct dma_fence *parent) 66 { 67 /* Set the parent before signaling the scheduled fence, such that, 68 * any waiter expecting the parent to be filled after the job has 69 * been scheduled (which is the case for drivers delegating waits 70 * to some firmware) doesn't have to busy wait for parent to show 71 * up. 72 */ 73 if (!IS_ERR_OR_NULL(parent)) 74 drm_sched_fence_set_parent(fence, parent); 75 76 dma_fence_signal(&fence->scheduled); 77 } 78 79 void drm_sched_fence_finished(struct drm_sched_fence *fence, int result) 80 { 81 if (result) 82 dma_fence_set_error(&fence->finished, result); 83 dma_fence_signal(&fence->finished); 84 } 85 86 static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence) 87 { 88 return "drm_sched"; 89 } 90 91 static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f) 92 { 93 struct drm_sched_fence *fence = to_drm_sched_fence(f); 94 return (const char *)fence->sched->name; 95 } 96 97 static void drm_sched_fence_free_rcu(struct rcu_head *rcu) 98 { 99 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu); 100 struct drm_sched_fence *fence = to_drm_sched_fence(f); 101 102 if (!WARN_ON_ONCE(!fence)) 103 kmem_cache_free(sched_fence_slab, fence); 104 } 105 106 /** 107 * drm_sched_fence_free - free up an uninitialized fence 108 * 109 * @fence: fence to free 110 * 111 * Free up the fence memory. Should only be used if drm_sched_fence_init() 112 * has not been called yet. 113 */ 114 void drm_sched_fence_free(struct drm_sched_fence *fence) 115 { 116 /* This function should not be called if the fence has been initialized. */ 117 if (!WARN_ON_ONCE(fence->sched)) 118 kmem_cache_free(sched_fence_slab, fence); 119 } 120 121 /** 122 * drm_sched_fence_release_scheduled - callback that fence can be freed 123 * 124 * @f: fence 125 * 126 * This function is called when the reference count becomes zero. 127 * It just RCU schedules freeing up the fence. 128 */ 129 static void drm_sched_fence_release_scheduled(struct dma_fence *f) 130 { 131 struct drm_sched_fence *fence = to_drm_sched_fence(f); 132 133 dma_fence_put(fence->parent); 134 call_rcu(&fence->finished.rcu, drm_sched_fence_free_rcu); 135 } 136 137 /** 138 * drm_sched_fence_release_finished - drop extra reference 139 * 140 * @f: fence 141 * 142 * Drop the extra reference from the scheduled fence to the base fence. 143 */ 144 static void drm_sched_fence_release_finished(struct dma_fence *f) 145 { 146 struct drm_sched_fence *fence = to_drm_sched_fence(f); 147 148 dma_fence_put(&fence->scheduled); 149 } 150 151 static void drm_sched_fence_set_deadline_finished(struct dma_fence *f, 152 ktime_t deadline) 153 { 154 struct drm_sched_fence *fence = to_drm_sched_fence(f); 155 struct dma_fence *parent; 156 unsigned long flags; 157 158 spin_lock_irqsave(&fence->lock, flags); 159 160 /* If we already have an earlier deadline, keep it: */ 161 if (test_bit(DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT, &f->flags) && 162 ktime_before(fence->deadline, deadline)) { 163 spin_unlock_irqrestore(&fence->lock, flags); 164 return; 165 } 166 167 fence->deadline = deadline; 168 set_bit(DRM_SCHED_FENCE_FLAG_HAS_DEADLINE_BIT, &f->flags); 169 170 spin_unlock_irqrestore(&fence->lock, flags); 171 172 /* 173 * smp_load_aquire() to ensure that if we are racing another 174 * thread calling drm_sched_fence_set_parent(), that we see 175 * the parent set before it calls test_bit(HAS_DEADLINE_BIT) 176 */ 177 parent = smp_load_acquire(&fence->parent); 178 if (parent) 179 dma_fence_set_deadline(parent, deadline); 180 } 181 182 static const struct dma_fence_ops drm_sched_fence_ops_scheduled = { 183 .get_driver_name = drm_sched_fence_get_driver_name, 184 .get_timeline_name = drm_sched_fence_get_timeline_name, 185 .release = drm_sched_fence_release_scheduled, 186 }; 187 188 static const struct dma_fence_ops drm_sched_fence_ops_finished = { 189 .get_driver_name = drm_sched_fence_get_driver_name, 190 .get_timeline_name = drm_sched_fence_get_timeline_name, 191 .release = drm_sched_fence_release_finished, 192 .set_deadline = drm_sched_fence_set_deadline_finished, 193 }; 194 195 struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f) 196 { 197 if (f->ops == &drm_sched_fence_ops_scheduled) 198 return container_of(f, struct drm_sched_fence, scheduled); 199 200 if (f->ops == &drm_sched_fence_ops_finished) 201 return container_of(f, struct drm_sched_fence, finished); 202 203 return NULL; 204 } 205 EXPORT_SYMBOL(to_drm_sched_fence); 206 207 struct drm_sched_fence *drm_sched_fence_alloc(struct drm_sched_entity *entity, 208 void *owner) 209 { 210 struct drm_sched_fence *fence = NULL; 211 212 fence = kmem_cache_zalloc(sched_fence_slab, GFP_KERNEL); 213 if (fence == NULL) 214 return NULL; 215 216 fence->owner = owner; 217 spin_lock_init(&fence->lock); 218 219 return fence; 220 } 221 222 void drm_sched_fence_init(struct drm_sched_fence *fence, 223 struct drm_sched_entity *entity) 224 { 225 unsigned seq; 226 227 fence->sched = entity->rq->sched; 228 seq = atomic_inc_return(&entity->fence_seq); 229 dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled, 230 &fence->lock, entity->fence_context, seq); 231 dma_fence_init(&fence->finished, &drm_sched_fence_ops_finished, 232 &fence->lock, entity->fence_context + 1, seq); 233 } 234 235 module_init(drm_sched_fence_slab_init); 236 module_exit(drm_sched_fence_slab_fini); 237 238 MODULE_DESCRIPTION("DRM GPU scheduler"); 239 MODULE_LICENSE("GPL and additional rights"); 240