Home | History | Annotate | Line # | Download | only in scheduler
      1  1.7  riastrad /*	$NetBSD: sched_fence.c,v 1.7 2022/07/28 10:44:46 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2015 Advanced Micro Devices, Inc.
      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.1  riastrad  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.7  riastrad __KERNEL_RCSID(0, "$NetBSD: sched_fence.c,v 1.7 2022/07/28 10:44:46 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/kthread.h>
     30  1.1  riastrad #include <linux/module.h>
     31  1.1  riastrad #include <linux/sched.h>
     32  1.1  riastrad #include <linux/slab.h>
     33  1.1  riastrad #include <linux/wait.h>
     34  1.1  riastrad 
     35  1.1  riastrad #include <drm/gpu_scheduler.h>
     36  1.1  riastrad 
     37  1.1  riastrad static struct kmem_cache *sched_fence_slab;
     38  1.1  riastrad 
     39  1.3  riastrad #ifdef __NetBSD__		/* XXX module init/fini */
     40  1.3  riastrad #define	__init
     41  1.3  riastrad #define	__exit
     42  1.3  riastrad #endif
     43  1.3  riastrad 
     44  1.1  riastrad static int __init drm_sched_fence_slab_init(void)
     45  1.1  riastrad {
     46  1.1  riastrad 	sched_fence_slab = kmem_cache_create(
     47  1.1  riastrad 		"drm_sched_fence", sizeof(struct drm_sched_fence), 0,
     48  1.1  riastrad 		SLAB_HWCACHE_ALIGN, NULL);
     49  1.1  riastrad 	if (!sched_fence_slab)
     50  1.1  riastrad 		return -ENOMEM;
     51  1.1  riastrad 
     52  1.1  riastrad 	return 0;
     53  1.1  riastrad }
     54  1.1  riastrad 
     55  1.1  riastrad static void __exit drm_sched_fence_slab_fini(void)
     56  1.1  riastrad {
     57  1.1  riastrad 	rcu_barrier();
     58  1.1  riastrad 	kmem_cache_destroy(sched_fence_slab);
     59  1.1  riastrad }
     60  1.1  riastrad 
     61  1.1  riastrad void drm_sched_fence_scheduled(struct drm_sched_fence *fence)
     62  1.1  riastrad {
     63  1.1  riastrad 	int ret = dma_fence_signal(&fence->scheduled);
     64  1.1  riastrad 
     65  1.1  riastrad 	if (!ret)
     66  1.1  riastrad 		DMA_FENCE_TRACE(&fence->scheduled,
     67  1.1  riastrad 				"signaled from irq context\n");
     68  1.1  riastrad 	else
     69  1.1  riastrad 		DMA_FENCE_TRACE(&fence->scheduled,
     70  1.1  riastrad 				"was already signaled\n");
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad void drm_sched_fence_finished(struct drm_sched_fence *fence)
     74  1.1  riastrad {
     75  1.1  riastrad 	int ret = dma_fence_signal(&fence->finished);
     76  1.1  riastrad 
     77  1.1  riastrad 	if (!ret)
     78  1.1  riastrad 		DMA_FENCE_TRACE(&fence->finished,
     79  1.1  riastrad 				"signaled from irq context\n");
     80  1.1  riastrad 	else
     81  1.1  riastrad 		DMA_FENCE_TRACE(&fence->finished,
     82  1.1  riastrad 				"was already signaled\n");
     83  1.1  riastrad }
     84  1.1  riastrad 
     85  1.1  riastrad static const char *drm_sched_fence_get_driver_name(struct dma_fence *fence)
     86  1.1  riastrad {
     87  1.1  riastrad 	return "drm_sched";
     88  1.1  riastrad }
     89  1.1  riastrad 
     90  1.1  riastrad static const char *drm_sched_fence_get_timeline_name(struct dma_fence *f)
     91  1.1  riastrad {
     92  1.1  riastrad 	struct drm_sched_fence *fence = to_drm_sched_fence(f);
     93  1.1  riastrad 	return (const char *)fence->sched->name;
     94  1.1  riastrad }
     95  1.1  riastrad 
     96  1.1  riastrad /**
     97  1.1  riastrad  * drm_sched_fence_free - free up the fence memory
     98  1.1  riastrad  *
     99  1.1  riastrad  * @rcu: RCU callback head
    100  1.1  riastrad  *
    101  1.1  riastrad  * Free up the fence memory after the RCU grace period.
    102  1.1  riastrad  */
    103  1.1  riastrad static void drm_sched_fence_free(struct rcu_head *rcu)
    104  1.1  riastrad {
    105  1.1  riastrad 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
    106  1.1  riastrad 	struct drm_sched_fence *fence = to_drm_sched_fence(f);
    107  1.1  riastrad 
    108  1.5  riastrad 	spin_lock_destroy(&fence->lock);
    109  1.5  riastrad 
    110  1.1  riastrad 	kmem_cache_free(sched_fence_slab, fence);
    111  1.1  riastrad }
    112  1.1  riastrad 
    113  1.1  riastrad /**
    114  1.1  riastrad  * drm_sched_fence_release_scheduled - callback that fence can be freed
    115  1.1  riastrad  *
    116  1.1  riastrad  * @fence: fence
    117  1.1  riastrad  *
    118  1.1  riastrad  * This function is called when the reference count becomes zero.
    119  1.1  riastrad  * It just RCU schedules freeing up the fence.
    120  1.1  riastrad  */
    121  1.1  riastrad static void drm_sched_fence_release_scheduled(struct dma_fence *f)
    122  1.1  riastrad {
    123  1.1  riastrad 	struct drm_sched_fence *fence = to_drm_sched_fence(f);
    124  1.1  riastrad 
    125  1.1  riastrad 	dma_fence_put(fence->parent);
    126  1.1  riastrad 	call_rcu(&fence->finished.rcu, drm_sched_fence_free);
    127  1.1  riastrad }
    128  1.1  riastrad 
    129  1.1  riastrad /**
    130  1.1  riastrad  * drm_sched_fence_release_finished - drop extra reference
    131  1.1  riastrad  *
    132  1.1  riastrad  * @f: fence
    133  1.1  riastrad  *
    134  1.1  riastrad  * Drop the extra reference from the scheduled fence to the base fence.
    135  1.1  riastrad  */
    136  1.1  riastrad static void drm_sched_fence_release_finished(struct dma_fence *f)
    137  1.1  riastrad {
    138  1.1  riastrad 	struct drm_sched_fence *fence = to_drm_sched_fence(f);
    139  1.1  riastrad 
    140  1.1  riastrad 	dma_fence_put(&fence->scheduled);
    141  1.1  riastrad }
    142  1.1  riastrad 
    143  1.1  riastrad static const struct dma_fence_ops drm_sched_fence_ops_scheduled = {
    144  1.1  riastrad 	.get_driver_name = drm_sched_fence_get_driver_name,
    145  1.1  riastrad 	.get_timeline_name = drm_sched_fence_get_timeline_name,
    146  1.1  riastrad 	.release = drm_sched_fence_release_scheduled,
    147  1.1  riastrad };
    148  1.1  riastrad 
    149  1.1  riastrad static const struct dma_fence_ops drm_sched_fence_ops_finished = {
    150  1.1  riastrad 	.get_driver_name = drm_sched_fence_get_driver_name,
    151  1.1  riastrad 	.get_timeline_name = drm_sched_fence_get_timeline_name,
    152  1.1  riastrad 	.release = drm_sched_fence_release_finished,
    153  1.1  riastrad };
    154  1.1  riastrad 
    155  1.1  riastrad struct drm_sched_fence *to_drm_sched_fence(struct dma_fence *f)
    156  1.1  riastrad {
    157  1.1  riastrad 	if (f->ops == &drm_sched_fence_ops_scheduled)
    158  1.1  riastrad 		return container_of(f, struct drm_sched_fence, scheduled);
    159  1.1  riastrad 
    160  1.1  riastrad 	if (f->ops == &drm_sched_fence_ops_finished)
    161  1.1  riastrad 		return container_of(f, struct drm_sched_fence, finished);
    162  1.1  riastrad 
    163  1.1  riastrad 	return NULL;
    164  1.1  riastrad }
    165  1.1  riastrad EXPORT_SYMBOL(to_drm_sched_fence);
    166  1.1  riastrad 
    167  1.1  riastrad struct drm_sched_fence *drm_sched_fence_create(struct drm_sched_entity *entity,
    168  1.1  riastrad 					       void *owner)
    169  1.1  riastrad {
    170  1.1  riastrad 	struct drm_sched_fence *fence = NULL;
    171  1.1  riastrad 	unsigned seq;
    172  1.1  riastrad 
    173  1.1  riastrad 	fence = kmem_cache_zalloc(sched_fence_slab, GFP_KERNEL);
    174  1.1  riastrad 	if (fence == NULL)
    175  1.1  riastrad 		return NULL;
    176  1.1  riastrad 
    177  1.1  riastrad 	fence->owner = owner;
    178  1.1  riastrad 	fence->sched = entity->rq->sched;
    179  1.1  riastrad 	spin_lock_init(&fence->lock);
    180  1.1  riastrad 
    181  1.1  riastrad 	seq = atomic_inc_return(&entity->fence_seq);
    182  1.1  riastrad 	dma_fence_init(&fence->scheduled, &drm_sched_fence_ops_scheduled,
    183  1.1  riastrad 		       &fence->lock, entity->fence_context, seq);
    184  1.1  riastrad 	dma_fence_init(&fence->finished, &drm_sched_fence_ops_finished,
    185  1.1  riastrad 		       &fence->lock, entity->fence_context + 1, seq);
    186  1.1  riastrad 
    187  1.1  riastrad 	return fence;
    188  1.1  riastrad }
    189  1.1  riastrad 
    190  1.1  riastrad module_init(drm_sched_fence_slab_init);
    191  1.1  riastrad module_exit(drm_sched_fence_slab_fini);
    192  1.1  riastrad 
    193  1.1  riastrad MODULE_DESCRIPTION("DRM GPU scheduler");
    194  1.1  riastrad MODULE_LICENSE("GPL and additional rights");
    195  1.3  riastrad 
    196  1.3  riastrad #ifdef __NetBSD__
    197  1.6  riastrad MODULE(MODULE_CLASS_MISC, drmkms_sched, "drmkms,drmkms_linux");
    198  1.6  riastrad 
    199  1.3  riastrad static int
    200  1.7  riastrad drmkms_sched_modcmd(modcmd_t cmd, void *arg)
    201  1.3  riastrad {
    202  1.3  riastrad 
    203  1.3  riastrad 	switch (cmd) {
    204  1.3  riastrad 	case MODULE_CMD_INIT:
    205  1.3  riastrad 		return drm_sched_fence_slab_init();
    206  1.3  riastrad 	case MODULE_CMD_FINI:
    207  1.3  riastrad 		drm_sched_fence_slab_fini();
    208  1.3  riastrad 		return 0;
    209  1.3  riastrad 	case MODULE_CMD_AUTOUNLOAD:
    210  1.3  riastrad 		return EBUSY;
    211  1.3  riastrad 	default:
    212  1.3  riastrad 		return ENOTTY;
    213  1.3  riastrad 	}
    214  1.3  riastrad }
    215  1.3  riastrad #endif
    216