drm/nouveau/fence: convert to exec engine, and improve channel sync
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nouveau_fence.c
CommitLineData
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
6ee73861 33#include "nouveau_drv.h"
0c6c1c2f 34#include "nouveau_ramht.h"
d375e7d5 35#include "nouveau_fence.h"
20abd163 36#include "nouveau_software.h"
6ee73861
BS
37#include "nouveau_dma.h"
38
5e120f6e
BS
39void
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}
6ee73861 60
6ee73861
BS
61void
62nouveau_fence_update(struct nouveau_channel *chan)
63{
2730723b 64 struct drm_device *dev = chan->dev;
5e120f6e
BS
65 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;
6ee73861 68
5e120f6e
BS
69 spin_lock(&fctx->lock);
70 list_for_each_entry_safe(fence, fnext, &fctx->pending, head) {
71 if (priv->read(chan) < fence->sequence)
b08abd4e
BS
72 break;
73
b08abd4e 74 if (fence->work)
8ac3891b 75 fence->work(fence->priv, true);
5e120f6e
BS
76 fence->channel = NULL;
77 list_del(&fence->head);
d375e7d5 78 nouveau_fence_unref(&fence);
6ee73861 79 }
5e120f6e 80 spin_unlock(&fctx->lock);
6ee73861
BS
81}
82
83int
d375e7d5 84nouveau_fence_emit(struct nouveau_fence *fence, struct nouveau_channel *chan)
6ee73861 85{
2730723b 86 struct drm_device *dev = chan->dev;
5e120f6e
BS
87 struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
88 struct nouveau_fence_chan *fctx = chan->engctx[NVOBJ_ENGINE_FENCE];
6ee73861
BS
89 int ret;
90
5e120f6e
BS
91 fence->channel = chan;
92 fence->timeout = jiffies + (3 * DRM_HZ);
93 fence->sequence = ++fctx->sequence;
6ee73861 94
5e120f6e
BS
95 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);
529c4959 101 }
6ee73861 102
5e120f6e 103 return ret;
6ee73861
BS
104}
105
6ee73861 106bool
d375e7d5 107nouveau_fence_done(struct nouveau_fence *fence)
6ee73861 108{
d375e7d5
BS
109 if (fence->channel)
110 nouveau_fence_update(fence->channel);
111 return !fence->channel;
6ee73861
BS
112}
113
114int
875ac34a 115nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr)
6ee73861 116{
bd35fe5a
MS
117 unsigned long sleep_time = NSEC_PER_MSEC / 1000;
118 ktime_t t;
6ee73861
BS
119 int ret = 0;
120
d375e7d5
BS
121 while (!nouveau_fence_done(fence)) {
122 if (fence->timeout && time_after_eq(jiffies, fence->timeout)) {
6ee73861
BS
123 ret = -EBUSY;
124 break;
125 }
126
875ac34a
BS
127 __set_current_state(intr ? TASK_INTERRUPTIBLE :
128 TASK_UNINTERRUPTIBLE);
bd35fe5a
MS
129 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 }
6ee73861
BS
136
137 if (intr && signal_pending(current)) {
9ddc8c52 138 ret = -ERESTARTSYS;
6ee73861
BS
139 break;
140 }
141 }
142
143 __set_current_state(TASK_RUNNING);
d375e7d5
BS
144 return ret;
145}
146
5e120f6e
BS
147int
148nouveau_fence_sync(struct nouveau_fence *fence, struct nouveau_channel *chan)
149{
150 struct nouveau_channel *prev = fence ? fence->channel : NULL;
151 struct drm_device *dev = chan->dev;
152 struct nouveau_fence_priv *priv = nv_engine(dev, NVOBJ_ENGINE_FENCE);
153 int ret = 0;
154
155 if (unlikely(prev && prev != chan && !nouveau_fence_done(fence))) {
156 ret = priv->sync(fence, chan);
157 if (unlikely(ret))
158 ret = nouveau_fence_wait(fence, true, false);
159 }
160
161 return ret;
162}
163
d375e7d5
BS
164static void
165nouveau_fence_del(struct kref *kref)
166{
167 struct nouveau_fence *fence = container_of(kref, typeof(*fence), kref);
168 kfree(fence);
169}
170
171void
172nouveau_fence_unref(struct nouveau_fence **pfence)
173{
174 if (*pfence)
175 kref_put(&(*pfence)->kref, nouveau_fence_del);
176 *pfence = NULL;
177}
178
179struct nouveau_fence *
180nouveau_fence_ref(struct nouveau_fence *fence)
181{
182 kref_get(&fence->kref);
183 return fence;
184}
185
186int
187nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence)
188{
189 struct nouveau_fence *fence;
190 int ret = 0;
6ee73861 191
5e120f6e
BS
192 if (unlikely(!chan->engctx[NVOBJ_ENGINE_FENCE]))
193 return -ENODEV;
194
d375e7d5
BS
195 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
196 if (!fence)
197 return -ENOMEM;
198 kref_init(&fence->kref);
199
200 if (chan) {
201 ret = nouveau_fence_emit(fence, chan);
202 if (ret)
203 nouveau_fence_unref(&fence);
204 }
205
206 *pfence = fence;
6ee73861
BS
207 return ret;
208}
This page took 0.213247 seconds and 5 git commands to generate.