Commit | Line | Data |
---|---|---|
6ee73861 BS |
1 | /* |
2 | * Copyright (C) 2007 Ben Skeggs. | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining | |
6 | * a copy of this software and associated documentation files (the | |
7 | * "Software"), to deal in the Software without restriction, including | |
8 | * without limitation the rights to use, copy, modify, merge, publish, | |
9 | * distribute, sublicense, and/or sell copies of the Software, and to | |
10 | * permit persons to whom the Software is furnished to do so, subject to | |
11 | * the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice (including the | |
14 | * next paragraph) shall be included in all copies or substantial | |
15 | * portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
20 | * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE | |
21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
24 | * | |
25 | */ | |
26 | ||
27 | #include "drmP.h" | |
28 | #include "drm.h" | |
29 | ||
bd35fe5a MS |
30 | #include <linux/ktime.h> |
31 | #include <linux/hrtimer.h> | |
32 | ||
ebb945a9 | 33 | #include "nouveau_drm.h" |
6ee73861 | 34 | #include "nouveau_dma.h" |
ebb945a9 | 35 | #include "nouveau_fence.h" |
6ee73861 | 36 | |
5e120f6e BS |
37 | void |
38 | nouveau_fence_context_del(struct nouveau_fence_chan *fctx) | |
39 | { | |
40 | struct nouveau_fence *fence, *fnext; | |
41 | spin_lock(&fctx->lock); | |
42 | list_for_each_entry_safe(fence, fnext, &fctx->pending, head) { | |
43 | if (fence->work) | |
44 | fence->work(fence->priv, false); | |
45 | fence->channel = NULL; | |
46 | list_del(&fence->head); | |
47 | nouveau_fence_unref(&fence); | |
48 | } | |
49 | spin_unlock(&fctx->lock); | |
50 | } | |
51 | ||
52 | void | |
53 | nouveau_fence_context_new(struct nouveau_fence_chan *fctx) | |
54 | { | |
f589be88 | 55 | INIT_LIST_HEAD(&fctx->flip); |
5e120f6e BS |
56 | INIT_LIST_HEAD(&fctx->pending); |
57 | spin_lock_init(&fctx->lock); | |
58 | } | |
6ee73861 | 59 | |
ebb945a9 | 60 | static void |
6ee73861 BS |
61 | nouveau_fence_update(struct nouveau_channel *chan) |
62 | { | |
ebb945a9 | 63 | struct nouveau_fence_priv *priv = chan->drm->fence; |
e193b1d4 | 64 | struct nouveau_fence_chan *fctx = chan->fence; |
5e120f6e | 65 | struct nouveau_fence *fence, *fnext; |
6ee73861 | 66 | |
5e120f6e BS |
67 | spin_lock(&fctx->lock); |
68 | list_for_each_entry_safe(fence, fnext, &fctx->pending, head) { | |
69 | if (priv->read(chan) < fence->sequence) | |
b08abd4e BS |
70 | break; |
71 | ||
b08abd4e | 72 | if (fence->work) |
8ac3891b | 73 | fence->work(fence->priv, true); |
5e120f6e BS |
74 | fence->channel = NULL; |
75 | list_del(&fence->head); | |
d375e7d5 | 76 | nouveau_fence_unref(&fence); |
6ee73861 | 77 | } |
5e120f6e | 78 | spin_unlock(&fctx->lock); |
6ee73861 BS |
79 | } |
80 | ||
81 | int | |
d375e7d5 | 82 | nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan) |
6ee73861 | 83 | { |
ebb945a9 | 84 | struct nouveau_fence_priv *priv = chan->drm->fence; |
e193b1d4 | 85 | struct nouveau_fence_chan *fctx = chan->fence; |
6ee73861 BS |
86 | int ret; |
87 | ||
5e120f6e BS |
88 | fence->channel = chan; |
89 | fence->timeout = jiffies + (3 * DRM_HZ); | |
90 | fence->sequence = ++fctx->sequence; | |
6ee73861 | 91 | |
5e120f6e BS |
92 | ret = priv->emit(fence); |
93 | if (!ret) { | |
94 | kref_get(&fence->kref); | |
95 | spin_lock(&fctx->lock); | |
96 | list_add_tail(&fence->head, &fctx->pending); | |
97 | spin_unlock(&fctx->lock); | |
529c4959 | 98 | } |
6ee73861 | 99 | |
5e120f6e | 100 | return ret; |
6ee73861 BS |
101 | } |
102 | ||
6ee73861 | 103 | bool |
d375e7d5 | 104 | nouveau_fence_done(struct nouveau_fence *fence) |
6ee73861 | 105 | { |
d375e7d5 BS |
106 | if (fence->channel) |
107 | nouveau_fence_update(fence->channel); | |
108 | return !fence->channel; | |
6ee73861 BS |
109 | } |
110 | ||
111 | int | |
875ac34a | 112 | nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr) |
6ee73861 | 113 | { |
bd35fe5a MS |
114 | unsigned long sleep_time = NSEC_PER_MSEC / 1000; |
115 | ktime_t t; | |
6ee73861 BS |
116 | int ret = 0; |
117 | ||
d375e7d5 BS |
118 | while (!nouveau_fence_done(fence)) { |
119 | if (fence->timeout && time_after_eq(jiffies, fence->timeout)) { | |
6ee73861 BS |
120 | ret = -EBUSY; |
121 | break; | |
122 | } | |
123 | ||
875ac34a BS |
124 | __set_current_state(intr ? TASK_INTERRUPTIBLE : |
125 | TASK_UNINTERRUPTIBLE); | |
bd35fe5a MS |
126 | if (lazy) { |
127 | t = ktime_set(0, sleep_time); | |
128 | schedule_hrtimeout(&t, HRTIMER_MODE_REL); | |
129 | sleep_time *= 2; | |
130 | if (sleep_time > NSEC_PER_MSEC) | |
131 | sleep_time = NSEC_PER_MSEC; | |
132 | } | |
6ee73861 BS |
133 | |
134 | if (intr && signal_pending(current)) { | |
9ddc8c52 | 135 | ret = -ERESTARTSYS; |
6ee73861 BS |
136 | break; |
137 | } | |
138 | } | |
139 | ||
140 | __set_current_state(TASK_RUNNING); | |
d375e7d5 BS |
141 | return ret; |
142 | } | |
143 | ||
5e120f6e BS |
144 | int |
145 | nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan) | |
146 | { | |
ebb945a9 | 147 | struct nouveau_fence_priv *priv = chan->drm->fence; |
906c033e | 148 | struct nouveau_channel *prev; |
5e120f6e BS |
149 | int ret = 0; |
150 | ||
ebb945a9 | 151 | prev = fence ? fence->channel : NULL; |
906c033e BS |
152 | if (prev) { |
153 | if (unlikely(prev != chan && !nouveau_fence_done(fence))) { | |
154 | ret = priv->sync(fence, prev, chan); | |
155 | if (unlikely(ret)) | |
156 | ret = nouveau_fence_wait(fence, true, false); | |
157 | } | |
5e120f6e BS |
158 | } |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
d375e7d5 BS |
163 | static void |
164 | nouveau_fence_del(struct kref *kref) | |
165 | { | |
166 | struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref); | |
167 | kfree(fence); | |
168 | } | |
169 | ||
170 | void | |
171 | nouveau_fence_unref(struct nouveau_fence **pfence) | |
172 | { | |
173 | if (*pfence) | |
174 | kref_put(&(*pfence)->kref, nouveau_fence_del); | |
175 | *pfence = NULL; | |
176 | } | |
177 | ||
178 | struct nouveau_fence * | |
179 | nouveau_fence_ref(struct nouveau_fence *fence) | |
180 | { | |
181 | kref_get(&fence->kref); | |
182 | return fence; | |
183 | } | |
184 | ||
185 | int | |
186 | nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence) | |
187 | { | |
188 | struct nouveau_fence *fence; | |
189 | int ret = 0; | |
6ee73861 | 190 | |
e193b1d4 | 191 | if (unlikely(!chan->fence)) |
5e120f6e BS |
192 | return -ENODEV; |
193 | ||
d375e7d5 BS |
194 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
195 | if (!fence) | |
196 | return -ENOMEM; | |
197 | kref_init(&fence->kref); | |
198 | ||
199 | if (chan) { | |
200 | ret = nouveau_fence_emit(fence, chan); | |
201 | if (ret) | |
202 | nouveau_fence_unref(&fence); | |
203 | } | |
204 | ||
205 | *pfence = fence; | |
6ee73861 BS |
206 | return ret; |
207 | } |