blob: 3c180493dab828c286b39531905f9697330f3c8b [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
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
Marcin Slusarzbd35fe52011-03-09 14:22:19 +010030#include <linux/ktime.h>
31#include <linux/hrtimer.h>
32
Ben Skeggs6ee73862009-12-11 19:24:15 +100033#include "nouveau_drv.h"
Francisco Jerez0c6c1c22010-09-22 00:58:54 +020034#include "nouveau_ramht.h"
Ben Skeggsd375e7d52012-04-30 13:30:00 +100035#include "nouveau_fence.h"
Ben Skeggs20abd162012-04-30 11:33:43 -050036#include "nouveau_software.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100037#include "nouveau_dma.h"
38
Ben Skeggs5e120f62012-04-30 13:55:29 +100039void
40nouveau_fence_context_del(struct nouveau_fence_chan *fctx)
41{
42 struct nouveau_fence *fence, *fnext;
43 spin_lock(&fctx->lock);
44 list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
45 if (fence->work)
46 fence->work(fence->priv, false);
47 fence->channel = NULL;
48 list_del(&fence->head);
49 nouveau_fence_unref(&fence);
50 }
51 spin_unlock(&fctx->lock);
52}
53
54void
55nouveau_fence_context_new(struct nouveau_fence_chan *fctx)
56{
57 INIT_LIST_HEAD(&fctx->pending);
58 spin_lock_init(&fctx->lock);
59}
Ben Skeggs6ee73862009-12-11 19:24:15 +100060
Ben Skeggs6ee73862009-12-11 19:24:15 +100061void
62nouveau_fence_update(struct nouveau_channel *chan)
63{
Francisco Jerez27307232010-09-21 18:57:11 +020064 struct drm_device *dev = chan->dev;
Ben Skeggs5e120f62012-04-30 13:55:29 +100065 struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
66 struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE];
67 struct nouveau_fence *fence, *fnext;
Ben Skeggs6ee73862009-12-11 19:24:15 +100068
Ben Skeggs5e120f62012-04-30 13:55:29 +100069 spin_lock(&fctx->lock);
70 list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
71 if (priv->read(chan) < fence->sequence)
Ben Skeggsb08abd42012-03-21 13:51:03 +100072 break;
73
Ben Skeggsb08abd42012-03-21 13:51:03 +100074 if (fence->work)
Francisco Jerez8ac38912010-09-21 20:49:39 +020075 fence->work(fence->priv, true);
Ben Skeggs5e120f62012-04-30 13:55:29 +100076 fence->channel = NULL;
77 list_del(&fence->head);
Ben Skeggsd375e7d52012-04-30 13:30:00 +100078 nouveau_fence_unref(&fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +100079 }
Ben Skeggs5e120f62012-04-30 13:55:29 +100080 spin_unlock(&fctx->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +100081}
82
83int
Ben Skeggsd375e7d52012-04-30 13:30:00 +100084nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
Ben Skeggs6ee73862009-12-11 19:24:15 +100085{
Francisco Jerez27307232010-09-21 18:57:11 +020086 struct drm_device *dev = chan->dev;
Ben Skeggs5e120f62012-04-30 13:55:29 +100087 struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
88 struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE];
Ben Skeggs6ee73862009-12-11 19:24:15 +100089 int ret;
90
Ben Skeggs5e120f62012-04-30 13:55:29 +100091 fence->channel = chan;
92 fence->timeout = jiffies + (3 * DRM_HZ);
93 fence->sequence = ++fctx->sequence;
Ben Skeggs6ee73862009-12-11 19:24:15 +100094
Ben Skeggs5e120f62012-04-30 13:55:29 +100095 ret = priv->emit(fence);
96 if (!ret) {
97 kref_get(&fence->kref);
98 spin_lock(&fctx->lock);
99 list_add_tail(&fence->head, &fctx->pending);
100 spin_unlock(&fctx->lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000101 }
102
Ben Skeggs5e120f62012-04-30 13:55:29 +1000103 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000104}
105
Ben Skeggs6ee73862009-12-11 19:24:15 +1000106bool
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000107nouveau_fence_done(struct nouveau_fence *fence)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000108{
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000109 if (fence->channel)
110 nouveau_fence_update(fence->channel);
111 return !fence->channel;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000112}
113
114int
Ben Skeggs875ac342012-04-30 12:51:48 +1000115nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000116{
Marcin Slusarzbd35fe52011-03-09 14:22:19 +0100117 unsigned long sleep_time = NSEC_PER_MSEC / 1000;
118 ktime_t t;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000119 int ret = 0;
120
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000121 while (!nouveau_fence_done(fence)) {
122 if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000123 ret = -EBUSY;
124 break;
125 }
126
Ben Skeggs875ac342012-04-30 12:51:48 +1000127 __set_current_state(intr ? TASK_INTERRUPTIBLE :
128 TASK_UNINTERRUPTIBLE);
Marcin Slusarzbd35fe52011-03-09 14:22:19 +0100129 if (lazy) {
130 t = ktime_set(0, sleep_time);
131 schedule_hrtimeout(&t, HRTIMER_MODE_REL);
132 sleep_time *= 2;
133 if (sleep_time > NSEC_PER_MSEC)
134 sleep_time = NSEC_PER_MSEC;
135 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000136
137 if (intr && signal_pending(current)) {
Ben Skeggs9ddc8c52009-12-15 11:04:25 +1000138 ret = -ERESTARTSYS;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000139 break;
140 }
141 }
142
143 __set_current_state(TASK_RUNNING);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000144 return ret;
145}
146
Ben Skeggs5e120f62012-04-30 13:55:29 +1000147int
148nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
149{
Ben Skeggs5e120f62012-04-30 13:55:29 +1000150 struct drm_device *dev = chan->dev;
151 struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
Ben Skeggs906c0332012-05-04 16:25:47 +1000152 struct nouveau_channel *prev;
Ben Skeggs5e120f62012-04-30 13:55:29 +1000153 int ret = 0;
154
Ben Skeggs906c0332012-05-04 16:25:47 +1000155 prev = fence ? nouveau_channel_get_unlocked(fence->channel) : NULL;
156 if (prev) {
157 if (unlikely(prev != chan && !nouveau_fence_done(fence))) {
158 ret = priv->sync(fence, prev, chan);
159 if (unlikely(ret))
160 ret = nouveau_fence_wait(fence, true, false);
161 }
162 nouveau_channel_put_unlocked(&prev);
Ben Skeggs5e120f62012-04-30 13:55:29 +1000163 }
164
165 return ret;
166}
167
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000168static void
169nouveau_fence_del(struct kref *kref)
170{
171 struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
172 kfree(fence);
173}
174
175void
176nouveau_fence_unref(struct nouveau_fence **pfence)
177{
178 if (*pfence)
179 kref_put(&(*pfence)->kref, nouveau_fence_del);
180 *pfence = NULL;
181}
182
183struct nouveau_fence *
184nouveau_fence_ref(struct nouveau_fence *fence)
185{
186 kref_get(&fence->kref);
187 return fence;
188}
189
190int
191nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence)
192{
193 struct nouveau_fence *fence;
194 int ret = 0;
195
Ben Skeggs5e120f62012-04-30 13:55:29 +1000196 if (unlikely(!chan->engctx[NVOBJ_ENGINE_FENCE]))
197 return -ENODEV;
198
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000199 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
200 if (!fence)
201 return -ENOMEM;
202 kref_init(&fence->kref);
203
204 if (chan) {
205 ret = nouveau_fence_emit(fence, chan);
206 if (ret)
207 nouveau_fence_unref(&fence);
208 }
209
210 *pfence = fence;
211 return ret;
212}