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 | ||
760285e7 | 27 | #include <drm/drmP.h> |
6ee73861 | 28 | |
bd35fe5a MS |
29 | #include <linux/ktime.h> |
30 | #include <linux/hrtimer.h> | |
31 | ||
6ee73861 | 32 | #include "nouveau_drv.h" |
0c6c1c2f | 33 | #include "nouveau_ramht.h" |
d375e7d5 | 34 | #include "nouveau_fence.h" |
20abd163 | 35 | #include "nouveau_software.h" |
6ee73861 BS |
36 | #include "nouveau_dma.h" |
37 | ||
5e120f6e BS |
38 | void |
39 | nouveau_fence_context_del(struct nouveau_fence_chan *fctx) | |
40 | { | |
41 | struct nouveau_fence *fence, *fnext; | |
42 | spin_lock(&fctx->lock); | |
43 | list_for_each_entry_safe(fence, fnext, &fctx->pending, head) { | |
44 | if (fence->work) | |
45 | fence->work(fence->priv, false); | |
46 | fence->channel = NULL; | |
47 | list_del(&fence->head); | |
48 | nouveau_fence_unref(&fence); | |
49 | } | |
50 | spin_unlock(&fctx->lock); | |
51 | } | |
52 | ||
53 | void | |
54 | nouveau_fence_context_new(struct nouveau_fence_chan *fctx) | |
55 | { | |
56 | INIT_LIST_HEAD(&fctx->pending); | |
57 | spin_lock_init(&fctx->lock); | |
58 | } | |
6ee73861 | 59 | |
6ee73861 BS |
60 | void |
61 | nouveau_fence_update(struct nouveau_channel *chan) | |
62 | { | |
2730723b | 63 | struct drm_device *dev = chan->dev; |
5e120f6e BS |
64 | struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE); |
65 | struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE]; | |
66 | struct nouveau_fence *fence, *fnext; | |
6ee73861 | 67 | |
5e120f6e BS |
68 | spin_lock(&fctx->lock); |
69 | list_for_each_entry_safe(fence, fnext, &fctx->pending, head) { | |
70 | if (priv->read(chan) < fence->sequence) | |
b08abd4e BS |
71 | break; |
72 | ||
b08abd4e | 73 | if (fence->work) |
8ac3891b | 74 | fence->work(fence->priv, true); |
5e120f6e BS |
75 | fence->channel = NULL; |
76 | list_del(&fence->head); | |
d375e7d5 | 77 | nouveau_fence_unref(&fence); |
6ee73861 | 78 | } |
5e120f6e | 79 | spin_unlock(&fctx->lock); |
6ee73861 BS |
80 | } |
81 | ||
82 | int | |
d375e7d5 | 83 | nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan) |
6ee73861 | 84 | { |
2730723b | 85 | struct drm_device *dev = chan->dev; |
5e120f6e BS |
86 | struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE); |
87 | struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE]; | |
6ee73861 BS |
88 | int ret; |
89 | ||
5e120f6e BS |
90 | fence->channel = chan; |
91 | fence->timeout = jiffies + (3 * DRM_HZ); | |
92 | fence->sequence = ++fctx->sequence; | |
6ee73861 | 93 | |
5e120f6e BS |
94 | ret = priv->emit(fence); |
95 | if (!ret) { | |
96 | kref_get(&fence->kref); | |
97 | spin_lock(&fctx->lock); | |
98 | list_add_tail(&fence->head, &fctx->pending); | |
99 | spin_unlock(&fctx->lock); | |
529c4959 | 100 | } |
6ee73861 | 101 | |
5e120f6e | 102 | return ret; |
6ee73861 BS |
103 | } |
104 | ||
6ee73861 | 105 | bool |
d375e7d5 | 106 | nouveau_fence_done(struct nouveau_fence *fence) |
6ee73861 | 107 | { |
d375e7d5 BS |
108 | if (fence->channel) |
109 | nouveau_fence_update(fence->channel); | |
110 | return !fence->channel; | |
6ee73861 BS |
111 | } |
112 | ||
113 | int | |
875ac34a | 114 | nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr) |
6ee73861 | 115 | { |
bd35fe5a MS |
116 | unsigned long sleep_time = NSEC_PER_MSEC / 1000; |
117 | ktime_t t; | |
6ee73861 BS |
118 | int ret = 0; |
119 | ||
d375e7d5 BS |
120 | while (!nouveau_fence_done(fence)) { |
121 | if (fence->timeout && time_after_eq(jiffies, fence->timeout)) { | |
6ee73861 BS |
122 | ret = -EBUSY; |
123 | break; | |
124 | } | |
125 | ||
875ac34a BS |
126 | __set_current_state(intr ? TASK_INTERRUPTIBLE : |
127 | TASK_UNINTERRUPTIBLE); | |
bd35fe5a MS |
128 | if (lazy) { |
129 | t = ktime_set(0, sleep_time); | |
130 | schedule_hrtimeout(&t, HRTIMER_MODE_REL); | |
131 | sleep_time *= 2; | |
132 | if (sleep_time > NSEC_PER_MSEC) | |
133 | sleep_time = NSEC_PER_MSEC; | |
134 | } | |
6ee73861 BS |
135 | |
136 | if (intr && signal_pending(current)) { | |
9ddc8c52 | 137 | ret = -ERESTARTSYS; |
6ee73861 BS |
138 | break; |
139 | } | |
140 | } | |
141 | ||
142 | __set_current_state(TASK_RUNNING); | |
d375e7d5 BS |
143 | return ret; |
144 | } | |
145 | ||
5e120f6e BS |
146 | int |
147 | nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan) | |
148 | { | |
5e120f6e BS |
149 | struct drm_device *dev = chan->dev; |
150 | struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE); | |
906c033e | 151 | struct nouveau_channel *prev; |
5e120f6e BS |
152 | int ret = 0; |
153 | ||
906c033e BS |
154 | prev = fence ? nouveau_channel_get_unlocked(fence->channel) : NULL; |
155 | if (prev) { | |
156 | if (unlikely(prev != chan && !nouveau_fence_done(fence))) { | |
157 | ret = priv->sync(fence, prev, chan); | |
158 | if (unlikely(ret)) | |
159 | ret = nouveau_fence_wait(fence, true, false); | |
160 | } | |
161 | nouveau_channel_put_unlocked(&prev); | |
5e120f6e BS |
162 | } |
163 | ||
164 | return ret; | |
165 | } | |
166 | ||
d375e7d5 BS |
167 | static void |
168 | nouveau_fence_del(struct kref *kref) | |
169 | { | |
170 | struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref); | |
171 | kfree(fence); | |
172 | } | |
173 | ||
174 | void | |
175 | nouveau_fence_unref(struct nouveau_fence **pfence) | |
176 | { | |
177 | if (*pfence) | |
178 | kref_put(&(*pfence)->kref, nouveau_fence_del); | |
179 | *pfence = NULL; | |
180 | } | |
181 | ||
182 | struct nouveau_fence * | |
183 | nouveau_fence_ref(struct nouveau_fence *fence) | |
184 | { | |
185 | kref_get(&fence->kref); | |
186 | return fence; | |
187 | } | |
188 | ||
189 | int | |
190 | nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence) | |
191 | { | |
192 | struct nouveau_fence *fence; | |
193 | int ret = 0; | |
6ee73861 | 194 | |
5e120f6e BS |
195 | if (unlikely(!chan->engctx[NVOBJ_ENGINE_FENCE])) |
196 | return -ENODEV; | |
197 | ||
d375e7d5 BS |
198 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
199 | if (!fence) | |
200 | return -ENOMEM; | |
201 | kref_init(&fence->kref); | |
202 | ||
203 | if (chan) { | |
204 | ret = nouveau_fence_emit(fence, chan); | |
205 | if (ret) | |
206 | nouveau_fence_unref(&fence); | |
207 | } | |
208 | ||
209 | *pfence = fence; | |
6ee73861 BS |
210 | return ret; |
211 | } |