Merge branch 'for-linus' of git://neil.brown.name/md
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_fence.c
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
30 #include "nouveau_drv.h"
31 #include "nouveau_dma.h"
32
33 #define USE_REFCNT (dev_priv->card_type >= NV_10)
34
35 struct nouveau_fence {
36 struct nouveau_channel *channel;
37 struct kref refcount;
38 struct list_head entry;
39
40 uint32_t sequence;
41 bool signalled;
42 };
43
44 static inline struct nouveau_fence *
45 nouveau_fence(void *sync_obj)
46 {
47 return (struct nouveau_fence *)sync_obj;
48 }
49
50 static void
51 nouveau_fence_del(struct kref *ref)
52 {
53 struct nouveau_fence *fence =
54 container_of(ref, struct nouveau_fence, refcount);
55
56 kfree(fence);
57 }
58
59 void
60 nouveau_fence_update(struct nouveau_channel *chan)
61 {
62 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
63 struct list_head *entry, *tmp;
64 struct nouveau_fence *fence;
65 uint32_t sequence;
66
67 if (USE_REFCNT)
68 sequence = nvchan_rd32(chan, 0x48);
69 else
70 sequence = atomic_read(&chan->fence.last_sequence_irq);
71
72 if (chan->fence.sequence_ack == sequence)
73 return;
74 chan->fence.sequence_ack = sequence;
75
76 spin_lock(&chan->fence.lock);
77 list_for_each_safe(entry, tmp, &chan->fence.pending) {
78 fence = list_entry(entry, struct nouveau_fence, entry);
79
80 sequence = fence->sequence;
81 fence->signalled = true;
82 list_del(&fence->entry);
83 kref_put(&fence->refcount, nouveau_fence_del);
84
85 if (sequence == chan->fence.sequence_ack)
86 break;
87 }
88 spin_unlock(&chan->fence.lock);
89 }
90
91 int
92 nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence,
93 bool emit)
94 {
95 struct nouveau_fence *fence;
96 int ret = 0;
97
98 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
99 if (!fence)
100 return -ENOMEM;
101 kref_init(&fence->refcount);
102 fence->channel = chan;
103
104 if (emit)
105 ret = nouveau_fence_emit(fence);
106
107 if (ret)
108 nouveau_fence_unref((void *)&fence);
109 *pfence = fence;
110 return ret;
111 }
112
113 struct nouveau_channel *
114 nouveau_fence_channel(struct nouveau_fence *fence)
115 {
116 return fence ? fence->channel : NULL;
117 }
118
119 int
120 nouveau_fence_emit(struct nouveau_fence *fence)
121 {
122 struct drm_nouveau_private *dev_priv = fence->channel->dev->dev_private;
123 struct nouveau_channel *chan = fence->channel;
124 int ret;
125
126 ret = RING_SPACE(chan, 2);
127 if (ret)
128 return ret;
129
130 if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) {
131 nouveau_fence_update(chan);
132
133 BUG_ON(chan->fence.sequence ==
134 chan->fence.sequence_ack - 1);
135 }
136
137 fence->sequence = ++chan->fence.sequence;
138
139 kref_get(&fence->refcount);
140 spin_lock(&chan->fence.lock);
141 list_add_tail(&fence->entry, &chan->fence.pending);
142 spin_unlock(&chan->fence.lock);
143
144 BEGIN_RING(chan, NvSubSw, USE_REFCNT ? 0x0050 : 0x0150, 1);
145 OUT_RING(chan, fence->sequence);
146 FIRE_RING(chan);
147
148 return 0;
149 }
150
151 void
152 nouveau_fence_unref(void **sync_obj)
153 {
154 struct nouveau_fence *fence = nouveau_fence(*sync_obj);
155
156 if (fence)
157 kref_put(&fence->refcount, nouveau_fence_del);
158 *sync_obj = NULL;
159 }
160
161 void *
162 nouveau_fence_ref(void *sync_obj)
163 {
164 struct nouveau_fence *fence = nouveau_fence(sync_obj);
165
166 kref_get(&fence->refcount);
167 return sync_obj;
168 }
169
170 bool
171 nouveau_fence_signalled(void *sync_obj, void *sync_arg)
172 {
173 struct nouveau_fence *fence = nouveau_fence(sync_obj);
174 struct nouveau_channel *chan = fence->channel;
175
176 if (fence->signalled)
177 return true;
178
179 nouveau_fence_update(chan);
180 return fence->signalled;
181 }
182
183 int
184 nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
185 {
186 unsigned long timeout = jiffies + (3 * DRM_HZ);
187 int ret = 0;
188
189 while (1) {
190 if (nouveau_fence_signalled(sync_obj, sync_arg))
191 break;
192
193 if (time_after_eq(jiffies, timeout)) {
194 ret = -EBUSY;
195 break;
196 }
197
198 __set_current_state(intr ? TASK_INTERRUPTIBLE
199 : TASK_UNINTERRUPTIBLE);
200 if (lazy)
201 schedule_timeout(1);
202
203 if (intr && signal_pending(current)) {
204 ret = -ERESTARTSYS;
205 break;
206 }
207 }
208
209 __set_current_state(TASK_RUNNING);
210
211 return ret;
212 }
213
214 int
215 nouveau_fence_flush(void *sync_obj, void *sync_arg)
216 {
217 return 0;
218 }
219
220 int
221 nouveau_fence_init(struct nouveau_channel *chan)
222 {
223 INIT_LIST_HEAD(&chan->fence.pending);
224 spin_lock_init(&chan->fence.lock);
225 atomic_set(&chan->fence.last_sequence_irq, 0);
226 return 0;
227 }
228
229 void
230 nouveau_fence_fini(struct nouveau_channel *chan)
231 {
232 struct list_head *entry, *tmp;
233 struct nouveau_fence *fence;
234
235 list_for_each_safe(entry, tmp, &chan->fence.pending) {
236 fence = list_entry(entry, struct nouveau_fence, entry);
237
238 fence->signalled = true;
239 list_del(&fence->entry);
240 kref_put(&fence->refcount, nouveau_fence_del);
241 }
242 }
243
This page took 0.041285 seconds and 5 git commands to generate.