GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / gpu / drm / amd / scheduler / sched_fence.c
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/kthread.h>
25 #include <linux/wait.h>
26 #include <linux/sched.h>
27 #include <drm/drmP.h>
28 #include "gpu_scheduler.h"
29
30 static struct kmem_cache *sched_fence_slab;
31
32 int amd_sched_fence_slab_init(void)
33 {
34         sched_fence_slab = kmem_cache_create(
35                 "amd_sched_fence", sizeof(struct amd_sched_fence), 0,
36                 SLAB_HWCACHE_ALIGN, NULL);
37         if (!sched_fence_slab)
38                 return -ENOMEM;
39
40         return 0;
41 }
42
43 void amd_sched_fence_slab_fini(void)
44 {
45         rcu_barrier();
46         kmem_cache_destroy(sched_fence_slab);
47 }
48
49 struct amd_sched_fence *amd_sched_fence_create(struct amd_sched_entity *entity,
50                                                void *owner)
51 {
52         struct amd_sched_fence *fence = NULL;
53         unsigned seq;
54
55         fence = kmem_cache_zalloc(sched_fence_slab, GFP_KERNEL);
56         if (fence == NULL)
57                 return NULL;
58
59         fence->owner = owner;
60         fence->sched = entity->sched;
61         spin_lock_init(&fence->lock);
62
63         seq = atomic_inc_return(&entity->fence_seq);
64         fence_init(&fence->scheduled, &amd_sched_fence_ops_scheduled,
65                    &fence->lock, entity->fence_context, seq);
66         fence_init(&fence->finished, &amd_sched_fence_ops_finished,
67                    &fence->lock, entity->fence_context + 1, seq);
68
69         return fence;
70 }
71
72 void amd_sched_fence_scheduled(struct amd_sched_fence *fence)
73 {
74         int ret = fence_signal(&fence->scheduled);
75
76         if (!ret)
77                 FENCE_TRACE(&fence->scheduled, "signaled from irq context\n");
78         else
79                 FENCE_TRACE(&fence->scheduled, "was already signaled\n");
80 }
81
82 void amd_sched_fence_finished(struct amd_sched_fence *fence)
83 {
84         int ret = fence_signal(&fence->finished);
85
86         if (!ret)
87                 FENCE_TRACE(&fence->finished, "signaled from irq context\n");
88         else
89                 FENCE_TRACE(&fence->finished, "was already signaled\n");
90 }
91
92 static const char *amd_sched_fence_get_driver_name(struct fence *fence)
93 {
94         return "amd_sched";
95 }
96
97 static const char *amd_sched_fence_get_timeline_name(struct fence *f)
98 {
99         struct amd_sched_fence *fence = to_amd_sched_fence(f);
100         return (const char *)fence->sched->name;
101 }
102
103 static bool amd_sched_fence_enable_signaling(struct fence *f)
104 {
105         return true;
106 }
107
108 /**
109  * amd_sched_fence_free - free up the fence memory
110  *
111  * @rcu: RCU callback head
112  *
113  * Free up the fence memory after the RCU grace period.
114  */
115 static void amd_sched_fence_free(struct rcu_head *rcu)
116 {
117         struct fence *f = container_of(rcu, struct fence, rcu);
118         struct amd_sched_fence *fence = to_amd_sched_fence(f);
119
120         fence_put(fence->parent);
121         kmem_cache_free(sched_fence_slab, fence);
122 }
123
124 /**
125  * amd_sched_fence_release_scheduled - callback that fence can be freed
126  *
127  * @fence: fence
128  *
129  * This function is called when the reference count becomes zero.
130  * It just RCU schedules freeing up the fence.
131  */
132 static void amd_sched_fence_release_scheduled(struct fence *f)
133 {
134         struct amd_sched_fence *fence = to_amd_sched_fence(f);
135
136         call_rcu(&fence->finished.rcu, amd_sched_fence_free);
137 }
138
139 /**
140  * amd_sched_fence_release_finished - drop extra reference
141  *
142  * @f: fence
143  *
144  * Drop the extra reference from the scheduled fence to the base fence.
145  */
146 static void amd_sched_fence_release_finished(struct fence *f)
147 {
148         struct amd_sched_fence *fence = to_amd_sched_fence(f);
149
150         fence_put(&fence->scheduled);
151 }
152
153 const struct fence_ops amd_sched_fence_ops_scheduled = {
154         .get_driver_name = amd_sched_fence_get_driver_name,
155         .get_timeline_name = amd_sched_fence_get_timeline_name,
156         .enable_signaling = amd_sched_fence_enable_signaling,
157         .signaled = NULL,
158         .wait = fence_default_wait,
159         .release = amd_sched_fence_release_scheduled,
160 };
161
162 const struct fence_ops amd_sched_fence_ops_finished = {
163         .get_driver_name = amd_sched_fence_get_driver_name,
164         .get_timeline_name = amd_sched_fence_get_timeline_name,
165         .enable_signaling = amd_sched_fence_enable_signaling,
166         .signaled = NULL,
167         .wait = fence_default_wait,
168         .release = amd_sched_fence_release_finished,
169 };