Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 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 | |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 30 | #include <linux/ktime.h> |
| 31 | #include <linux/hrtimer.h> |
| 32 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 33 | #include "nouveau_drv.h" |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 34 | #include "nouveau_ramht.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 35 | #include "nouveau_dma.h" |
| 36 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 37 | #define USE_REFCNT(dev) (nouveau_private(dev)->chipset >= 0x10) |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 38 | #define USE_SEMA(dev) (nouveau_private(dev)->chipset >= 0x17) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 39 | |
| 40 | struct nouveau_fence { |
| 41 | struct nouveau_channel *channel; |
| 42 | struct kref refcount; |
| 43 | struct list_head entry; |
| 44 | |
| 45 | uint32_t sequence; |
| 46 | bool signalled; |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 47 | |
| 48 | void (*work)(void *priv, bool signalled); |
| 49 | void *priv; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 50 | }; |
| 51 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 52 | struct nouveau_semaphore { |
| 53 | struct kref ref; |
| 54 | struct drm_device *dev; |
| 55 | struct drm_mm_node *mem; |
| 56 | }; |
| 57 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 58 | static inline struct nouveau_fence * |
| 59 | nouveau_fence(void *sync_obj) |
| 60 | { |
| 61 | return (struct nouveau_fence *)sync_obj; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | nouveau_fence_del(struct kref *ref) |
| 66 | { |
| 67 | struct nouveau_fence *fence = |
| 68 | container_of(ref, struct nouveau_fence, refcount); |
| 69 | |
Francisco Jerez | 2a6789a | 2010-10-18 03:56:14 +0200 | [diff] [blame] | 70 | nouveau_channel_ref(NULL, &fence->channel); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 71 | kfree(fence); |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | nouveau_fence_update(struct nouveau_channel *chan) |
| 76 | { |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 77 | struct drm_device *dev = chan->dev; |
| 78 | struct nouveau_fence *tmp, *fence; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 79 | uint32_t sequence; |
| 80 | |
Francisco Jerez | 3ba6462 | 2010-08-28 17:56:33 +0200 | [diff] [blame] | 81 | spin_lock(&chan->fence.lock); |
| 82 | |
Francisco Jerez | 937c347 | 2010-12-08 02:35:45 +0100 | [diff] [blame] | 83 | /* Fetch the last sequence if the channel is still up and running */ |
| 84 | if (likely(!list_empty(&chan->fence.pending))) { |
| 85 | if (USE_REFCNT(dev)) |
| 86 | sequence = nvchan_rd32(chan, 0x48); |
| 87 | else |
| 88 | sequence = atomic_read(&chan->fence.last_sequence_irq); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 89 | |
Francisco Jerez | 937c347 | 2010-12-08 02:35:45 +0100 | [diff] [blame] | 90 | if (chan->fence.sequence_ack == sequence) |
| 91 | goto out; |
| 92 | chan->fence.sequence_ack = sequence; |
| 93 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 94 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 95 | list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) { |
Ben Skeggs | b08abd4 | 2012-03-21 13:51:03 +1000 | [diff] [blame] | 96 | if (fence->sequence > chan->fence.sequence_ack) |
| 97 | break; |
| 98 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 99 | fence->signalled = true; |
| 100 | list_del(&fence->entry); |
Ben Skeggs | b08abd4 | 2012-03-21 13:51:03 +1000 | [diff] [blame] | 101 | if (fence->work) |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 102 | fence->work(fence->priv, true); |
| 103 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 104 | kref_put(&fence->refcount, nouveau_fence_del); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 105 | } |
Ben Skeggs | b08abd4 | 2012-03-21 13:51:03 +1000 | [diff] [blame] | 106 | |
Francisco Jerez | 3ba6462 | 2010-08-28 17:56:33 +0200 | [diff] [blame] | 107 | out: |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 108 | spin_unlock(&chan->fence.lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int |
| 112 | nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence, |
| 113 | bool emit) |
| 114 | { |
| 115 | struct nouveau_fence *fence; |
| 116 | int ret = 0; |
| 117 | |
| 118 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
| 119 | if (!fence) |
| 120 | return -ENOMEM; |
| 121 | kref_init(&fence->refcount); |
Francisco Jerez | 2a6789a | 2010-10-18 03:56:14 +0200 | [diff] [blame] | 122 | nouveau_channel_ref(chan, &fence->channel); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 123 | |
| 124 | if (emit) |
| 125 | ret = nouveau_fence_emit(fence); |
| 126 | |
| 127 | if (ret) |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 128 | nouveau_fence_unref(&fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 129 | *pfence = fence; |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | struct nouveau_channel * |
| 134 | nouveau_fence_channel(struct nouveau_fence *fence) |
| 135 | { |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 136 | return fence ? nouveau_channel_get_unlocked(fence->channel) : NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | int |
| 140 | nouveau_fence_emit(struct nouveau_fence *fence) |
| 141 | { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 142 | struct nouveau_channel *chan = fence->channel; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 143 | struct drm_device *dev = chan->dev; |
Ben Skeggs | 529c495 | 2010-11-24 10:30:22 +1000 | [diff] [blame] | 144 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 145 | int ret; |
| 146 | |
| 147 | ret = RING_SPACE(chan, 2); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | |
| 151 | if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 152 | nouveau_fence_update(chan); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 153 | |
| 154 | BUG_ON(chan->fence.sequence == |
| 155 | chan->fence.sequence_ack - 1); |
| 156 | } |
| 157 | |
| 158 | fence->sequence = ++chan->fence.sequence; |
| 159 | |
| 160 | kref_get(&fence->refcount); |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 161 | spin_lock(&chan->fence.lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 162 | list_add_tail(&fence->entry, &chan->fence.pending); |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 163 | spin_unlock(&chan->fence.lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 164 | |
Ben Skeggs | 529c495 | 2010-11-24 10:30:22 +1000 | [diff] [blame] | 165 | if (USE_REFCNT(dev)) { |
| 166 | if (dev_priv->card_type < NV_C0) |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 167 | BEGIN_RING(chan, 0, NV10_SUBCHAN_REF_CNT, 1); |
Ben Skeggs | 529c495 | 2010-11-24 10:30:22 +1000 | [diff] [blame] | 168 | else |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 169 | BEGIN_NVC0(chan, 2, 0, NV10_SUBCHAN_REF_CNT, 1); |
Ben Skeggs | 529c495 | 2010-11-24 10:30:22 +1000 | [diff] [blame] | 170 | } else { |
| 171 | BEGIN_RING(chan, NvSubSw, 0x0150, 1); |
| 172 | } |
| 173 | OUT_RING (chan, fence->sequence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 174 | FIRE_RING(chan); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | void |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 180 | nouveau_fence_work(struct nouveau_fence *fence, |
| 181 | void (*work)(void *priv, bool signalled), |
| 182 | void *priv) |
| 183 | { |
| 184 | BUG_ON(fence->work); |
| 185 | |
| 186 | spin_lock(&fence->channel->fence.lock); |
| 187 | |
| 188 | if (fence->signalled) { |
| 189 | work(priv, true); |
| 190 | } else { |
| 191 | fence->work = work; |
| 192 | fence->priv = priv; |
| 193 | } |
| 194 | |
| 195 | spin_unlock(&fence->channel->fence.lock); |
| 196 | } |
| 197 | |
| 198 | void |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 199 | __nouveau_fence_unref(void **sync_obj) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 200 | { |
| 201 | struct nouveau_fence *fence = nouveau_fence(*sync_obj); |
| 202 | |
| 203 | if (fence) |
| 204 | kref_put(&fence->refcount, nouveau_fence_del); |
| 205 | *sync_obj = NULL; |
| 206 | } |
| 207 | |
| 208 | void * |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 209 | __nouveau_fence_ref(void *sync_obj) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 210 | { |
| 211 | struct nouveau_fence *fence = nouveau_fence(sync_obj); |
| 212 | |
| 213 | kref_get(&fence->refcount); |
| 214 | return sync_obj; |
| 215 | } |
| 216 | |
| 217 | bool |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 218 | __nouveau_fence_signalled(void *sync_obj, void *sync_arg) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 219 | { |
| 220 | struct nouveau_fence *fence = nouveau_fence(sync_obj); |
| 221 | struct nouveau_channel *chan = fence->channel; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 222 | |
| 223 | if (fence->signalled) |
| 224 | return true; |
| 225 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 226 | nouveau_fence_update(chan); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 227 | return fence->signalled; |
| 228 | } |
| 229 | |
| 230 | int |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 231 | __nouveau_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 232 | { |
| 233 | unsigned long timeout = jiffies + (3 * DRM_HZ); |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 234 | unsigned long sleep_time = NSEC_PER_MSEC / 1000; |
| 235 | ktime_t t; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 236 | int ret = 0; |
| 237 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 238 | while (1) { |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 239 | if (__nouveau_fence_signalled(sync_obj, sync_arg)) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 240 | break; |
| 241 | |
| 242 | if (time_after_eq(jiffies, timeout)) { |
| 243 | ret = -EBUSY; |
| 244 | break; |
| 245 | } |
| 246 | |
Kulikov Vasiliy | 0599111 | 2010-07-26 12:23:54 +0400 | [diff] [blame] | 247 | __set_current_state(intr ? TASK_INTERRUPTIBLE |
| 248 | : TASK_UNINTERRUPTIBLE); |
Marcin Slusarz | bd35fe5 | 2011-03-09 14:22:19 +0100 | [diff] [blame] | 249 | if (lazy) { |
| 250 | t = ktime_set(0, sleep_time); |
| 251 | schedule_hrtimeout(&t, HRTIMER_MODE_REL); |
| 252 | sleep_time *= 2; |
| 253 | if (sleep_time > NSEC_PER_MSEC) |
| 254 | sleep_time = NSEC_PER_MSEC; |
| 255 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 256 | |
| 257 | if (intr && signal_pending(current)) { |
Ben Skeggs | 9ddc8c5 | 2009-12-15 11:04:25 +1000 | [diff] [blame] | 258 | ret = -ERESTARTSYS; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | __set_current_state(TASK_RUNNING); |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 268 | static struct nouveau_semaphore * |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 269 | semaphore_alloc(struct drm_device *dev) |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 270 | { |
| 271 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 272 | struct nouveau_semaphore *sema; |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 273 | int size = (dev_priv->chipset < 0x84) ? 4 : 16; |
| 274 | int ret, i; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 275 | |
| 276 | if (!USE_SEMA(dev)) |
| 277 | return NULL; |
| 278 | |
| 279 | sema = kmalloc(sizeof(*sema), GFP_KERNEL); |
| 280 | if (!sema) |
| 281 | goto fail; |
| 282 | |
Francisco Jerez | 907af60 | 2010-10-09 04:02:09 +0200 | [diff] [blame] | 283 | ret = drm_mm_pre_get(&dev_priv->fence.heap); |
| 284 | if (ret) |
| 285 | goto fail; |
| 286 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 287 | spin_lock(&dev_priv->fence.lock); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 288 | sema->mem = drm_mm_search_free(&dev_priv->fence.heap, size, 0, 0); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 289 | if (sema->mem) |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 290 | sema->mem = drm_mm_get_block_atomic(sema->mem, size, 0); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 291 | spin_unlock(&dev_priv->fence.lock); |
| 292 | |
| 293 | if (!sema->mem) |
| 294 | goto fail; |
| 295 | |
| 296 | kref_init(&sema->ref); |
| 297 | sema->dev = dev; |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 298 | for (i = sema->mem->start; i < sema->mem->start + size; i += 4) |
| 299 | nouveau_bo_wr32(dev_priv->fence.bo, i / 4, 0); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 300 | |
| 301 | return sema; |
| 302 | fail: |
| 303 | kfree(sema); |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | static void |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 308 | semaphore_free(struct kref *ref) |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 309 | { |
| 310 | struct nouveau_semaphore *sema = |
| 311 | container_of(ref, struct nouveau_semaphore, ref); |
| 312 | struct drm_nouveau_private *dev_priv = sema->dev->dev_private; |
| 313 | |
| 314 | spin_lock(&dev_priv->fence.lock); |
| 315 | drm_mm_put_block(sema->mem); |
| 316 | spin_unlock(&dev_priv->fence.lock); |
| 317 | |
| 318 | kfree(sema); |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | semaphore_work(void *priv, bool signalled) |
| 323 | { |
| 324 | struct nouveau_semaphore *sema = priv; |
| 325 | struct drm_nouveau_private *dev_priv = sema->dev->dev_private; |
| 326 | |
| 327 | if (unlikely(!signalled)) |
| 328 | nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 1); |
| 329 | |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 330 | kref_put(&sema->ref, semaphore_free); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 334 | semaphore_acquire(struct nouveau_channel *chan, struct nouveau_semaphore *sema) |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 335 | { |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 336 | struct drm_nouveau_private *dev_priv = chan->dev->dev_private; |
| 337 | struct nouveau_fence *fence = NULL; |
Ben Skeggs | d02836b | 2011-06-07 15:21:23 +1000 | [diff] [blame] | 338 | u64 offset = chan->fence.vma.offset + sema->mem->start; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 339 | int ret; |
| 340 | |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 341 | if (dev_priv->chipset < 0x84) { |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 342 | ret = RING_SPACE(chan, 4); |
Ben Skeggs | ec23802 | 2011-02-02 14:57:05 +1000 | [diff] [blame] | 343 | if (ret) |
| 344 | return ret; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 345 | |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 346 | BEGIN_RING(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 3); |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 347 | OUT_RING (chan, NvSema); |
Ben Skeggs | d02836b | 2011-06-07 15:21:23 +1000 | [diff] [blame] | 348 | OUT_RING (chan, offset); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 349 | OUT_RING (chan, 1); |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 350 | } else |
| 351 | if (dev_priv->chipset < 0xc0) { |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 352 | ret = RING_SPACE(chan, 7); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 353 | if (ret) |
| 354 | return ret; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 355 | |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 356 | BEGIN_RING(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1); |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 357 | OUT_RING (chan, chan->vram_handle); |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 358 | BEGIN_RING(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4); |
Ben Skeggs | e3b7ed5 | 2011-02-02 13:21:57 +1000 | [diff] [blame] | 359 | OUT_RING (chan, upper_32_bits(offset)); |
| 360 | OUT_RING (chan, lower_32_bits(offset)); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 361 | OUT_RING (chan, 1); |
| 362 | OUT_RING (chan, 1); /* ACQUIRE_EQ */ |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 363 | } else { |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 364 | ret = RING_SPACE(chan, 5); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 368 | BEGIN_NVC0(chan, 2, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4); |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 369 | OUT_RING (chan, upper_32_bits(offset)); |
| 370 | OUT_RING (chan, lower_32_bits(offset)); |
| 371 | OUT_RING (chan, 1); |
| 372 | OUT_RING (chan, 0x1001); /* ACQUIRE_EQ */ |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 375 | /* Delay semaphore destruction until its work is done */ |
| 376 | ret = nouveau_fence_new(chan, &fence, true); |
| 377 | if (ret) |
| 378 | return ret; |
| 379 | |
| 380 | kref_get(&sema->ref); |
| 381 | nouveau_fence_work(fence, semaphore_work, sema); |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 382 | nouveau_fence_unref(&fence); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 383 | return 0; |
| 384 | } |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 385 | |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 386 | static int |
| 387 | semaphore_release(struct nouveau_channel *chan, struct nouveau_semaphore *sema) |
| 388 | { |
| 389 | struct drm_nouveau_private *dev_priv = chan->dev->dev_private; |
| 390 | struct nouveau_fence *fence = NULL; |
Ben Skeggs | d02836b | 2011-06-07 15:21:23 +1000 | [diff] [blame] | 391 | u64 offset = chan->fence.vma.offset + sema->mem->start; |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 392 | int ret; |
| 393 | |
| 394 | if (dev_priv->chipset < 0x84) { |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 395 | ret = RING_SPACE(chan, 5); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 396 | if (ret) |
| 397 | return ret; |
| 398 | |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 399 | BEGIN_RING(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 2); |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 400 | OUT_RING (chan, NvSema); |
Ben Skeggs | d02836b | 2011-06-07 15:21:23 +1000 | [diff] [blame] | 401 | OUT_RING (chan, offset); |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 402 | BEGIN_RING(chan, 0, NV11_SUBCHAN_SEMAPHORE_RELEASE, 1); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 403 | OUT_RING (chan, 1); |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 404 | } else |
| 405 | if (dev_priv->chipset < 0xc0) { |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 406 | ret = RING_SPACE(chan, 7); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 407 | if (ret) |
| 408 | return ret; |
| 409 | |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 410 | BEGIN_RING(chan, 0, NV11_SUBCHAN_DMA_SEMAPHORE, 1); |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 411 | OUT_RING (chan, chan->vram_handle); |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 412 | BEGIN_RING(chan, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4); |
Ben Skeggs | e3b7ed5 | 2011-02-02 13:21:57 +1000 | [diff] [blame] | 413 | OUT_RING (chan, upper_32_bits(offset)); |
| 414 | OUT_RING (chan, lower_32_bits(offset)); |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 415 | OUT_RING (chan, 1); |
| 416 | OUT_RING (chan, 2); /* RELEASE */ |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 417 | } else { |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 418 | ret = RING_SPACE(chan, 5); |
| 419 | if (ret) |
| 420 | return ret; |
| 421 | |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 422 | BEGIN_NVC0(chan, 2, 0, NV84_SUBCHAN_SEMAPHORE_ADDRESS_HIGH, 4); |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 423 | OUT_RING (chan, upper_32_bits(offset)); |
| 424 | OUT_RING (chan, lower_32_bits(offset)); |
| 425 | OUT_RING (chan, 1); |
| 426 | OUT_RING (chan, 0x1002); /* RELEASE */ |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /* Delay semaphore destruction until its work is done */ |
| 430 | ret = nouveau_fence_new(chan, &fence, true); |
| 431 | if (ret) |
| 432 | return ret; |
| 433 | |
| 434 | kref_get(&sema->ref); |
| 435 | nouveau_fence_work(fence, semaphore_work, sema); |
| 436 | nouveau_fence_unref(&fence); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 437 | return 0; |
| 438 | } |
| 439 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 440 | int |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 441 | nouveau_fence_sync(struct nouveau_fence *fence, |
| 442 | struct nouveau_channel *wchan) |
| 443 | { |
| 444 | struct nouveau_channel *chan = nouveau_fence_channel(fence); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 445 | struct drm_device *dev = wchan->dev; |
| 446 | struct nouveau_semaphore *sema; |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 447 | int ret = 0; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 448 | |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 449 | if (likely(!chan || chan == wchan || |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 450 | nouveau_fence_signalled(fence))) |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 451 | goto out; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 452 | |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 453 | sema = semaphore_alloc(dev); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 454 | if (!sema) { |
| 455 | /* Early card or broken userspace, fall back to |
| 456 | * software sync. */ |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 457 | ret = nouveau_fence_wait(fence, true, false); |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 458 | goto out; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 459 | } |
| 460 | |
Ben Skeggs | 08cd3d4 | 2010-10-12 08:01:59 +1000 | [diff] [blame] | 461 | /* try to take chan's mutex, if we can't take it right away |
Ben Skeggs | cff5c13 | 2010-10-06 16:16:59 +1000 | [diff] [blame] | 462 | * we have to fallback to software sync to prevent locking |
| 463 | * order issues |
| 464 | */ |
Ben Skeggs | 08cd3d4 | 2010-10-12 08:01:59 +1000 | [diff] [blame] | 465 | if (!mutex_trylock(&chan->mutex)) { |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 466 | ret = nouveau_fence_wait(fence, true, false); |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 467 | goto out_unref; |
Ben Skeggs | cff5c13 | 2010-10-06 16:16:59 +1000 | [diff] [blame] | 468 | } |
| 469 | |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 470 | /* Make wchan wait until it gets signalled */ |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 471 | ret = semaphore_acquire(wchan, sema); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 472 | if (ret) |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 473 | goto out_unlock; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 474 | |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 475 | /* Signal the semaphore from chan */ |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 476 | ret = semaphore_release(chan, sema); |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 477 | |
| 478 | out_unlock: |
Ben Skeggs | 08cd3d4 | 2010-10-12 08:01:59 +1000 | [diff] [blame] | 479 | mutex_unlock(&chan->mutex); |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 480 | out_unref: |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 481 | kref_put(&sema->ref, semaphore_free); |
Francisco Jerez | 2b478add | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 482 | out: |
| 483 | if (chan) |
| 484 | nouveau_channel_put_unlocked(&chan); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 485 | return ret; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | int |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 489 | __nouveau_fence_flush(void *sync_obj, void *sync_arg) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 490 | { |
| 491 | return 0; |
| 492 | } |
| 493 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 494 | int |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 495 | nouveau_fence_channel_init(struct nouveau_channel *chan) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 496 | { |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 497 | struct drm_device *dev = chan->dev; |
| 498 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 499 | struct nouveau_gpuobj *obj = NULL; |
| 500 | int ret; |
| 501 | |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 502 | if (dev_priv->card_type < NV_C0) { |
| 503 | /* Create an NV_SW object for various sync purposes */ |
| 504 | ret = nouveau_gpuobj_gr_new(chan, NvSw, NV_SW); |
| 505 | if (ret) |
| 506 | return ret; |
Ben Skeggs | cb1d771 | 2011-01-28 13:44:32 +1000 | [diff] [blame] | 507 | |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 508 | ret = RING_SPACE(chan, 2); |
| 509 | if (ret) |
| 510 | return ret; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 511 | |
Ben Skeggs | b5b2e59 | 2012-03-16 00:09:54 +1000 | [diff] [blame] | 512 | BEGIN_RING(chan, NvSubSw, NV01_SUBCHAN_OBJECT, 1); |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 513 | OUT_RING (chan, NvSw); |
| 514 | FIRE_RING (chan); |
| 515 | } |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 516 | |
Ben Skeggs | b16a5a1 | 2011-06-17 23:41:54 +1000 | [diff] [blame] | 517 | /* Setup area of memory shared between all channels for x-chan sync */ |
Ben Skeggs | e3b7ed5 | 2011-02-02 13:21:57 +1000 | [diff] [blame] | 518 | if (USE_SEMA(dev) && dev_priv->chipset < 0x84) { |
Ben Skeggs | a8b214f | 2010-12-03 09:05:20 +1000 | [diff] [blame] | 519 | struct ttm_mem_reg *mem = &dev_priv->fence.bo->bo.mem; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 520 | |
Ben Skeggs | 91a8f1e | 2011-07-25 20:26:19 +1000 | [diff] [blame] | 521 | ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_FROM_MEMORY, |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 522 | mem->start << PAGE_SHIFT, |
Ben Skeggs | a8b214f | 2010-12-03 09:05:20 +1000 | [diff] [blame] | 523 | mem->size, NV_MEM_ACCESS_RW, |
Ben Skeggs | 7f4a195 | 2010-11-16 11:50:09 +1000 | [diff] [blame] | 524 | NV_MEM_TARGET_VRAM, &obj); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 525 | if (ret) |
| 526 | return ret; |
| 527 | |
| 528 | ret = nouveau_ramht_insert(chan, NvSema, obj); |
| 529 | nouveau_gpuobj_ref(NULL, &obj); |
| 530 | if (ret) |
| 531 | return ret; |
Ben Skeggs | cfd8be0 | 2011-08-23 10:23:11 +1000 | [diff] [blame] | 532 | } else |
| 533 | if (USE_SEMA(dev)) { |
Ben Skeggs | d02836b | 2011-06-07 15:21:23 +1000 | [diff] [blame] | 534 | /* map fence bo into channel's vm */ |
| 535 | ret = nouveau_bo_vma_add(dev_priv->fence.bo, chan->vm, |
| 536 | &chan->fence.vma); |
| 537 | if (ret) |
| 538 | return ret; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 539 | } |
| 540 | |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 541 | atomic_set(&chan->fence.last_sequence_irq, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | void |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 546 | nouveau_fence_channel_fini(struct nouveau_channel *chan) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 547 | { |
Ben Skeggs | d02836b | 2011-06-07 15:21:23 +1000 | [diff] [blame] | 548 | struct drm_nouveau_private *dev_priv = chan->dev->dev_private; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 549 | struct nouveau_fence *tmp, *fence; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 550 | |
Francisco Jerez | 889fa93 | 2010-10-18 03:57:19 +0200 | [diff] [blame] | 551 | spin_lock(&chan->fence.lock); |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 552 | list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 553 | fence->signalled = true; |
| 554 | list_del(&fence->entry); |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 555 | |
| 556 | if (unlikely(fence->work)) |
| 557 | fence->work(fence->priv, false); |
| 558 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 559 | kref_put(&fence->refcount, nouveau_fence_del); |
| 560 | } |
Francisco Jerez | 889fa93 | 2010-10-18 03:57:19 +0200 | [diff] [blame] | 561 | spin_unlock(&chan->fence.lock); |
Ben Skeggs | d02836b | 2011-06-07 15:21:23 +1000 | [diff] [blame] | 562 | |
| 563 | nouveau_bo_vma_del(dev_priv->fence.bo, &chan->fence.vma); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 564 | } |
| 565 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 566 | int |
| 567 | nouveau_fence_init(struct drm_device *dev) |
| 568 | { |
| 569 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Ben Skeggs | c3b90a7 | 2011-01-28 12:08:29 +1000 | [diff] [blame] | 570 | int size = (dev_priv->chipset < 0x84) ? 4096 : 16384; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 571 | int ret; |
| 572 | |
| 573 | /* Create a shared VRAM heap for cross-channel sync. */ |
| 574 | if (USE_SEMA(dev)) { |
Ben Skeggs | 7375c95 | 2011-06-07 14:21:29 +1000 | [diff] [blame] | 575 | ret = nouveau_bo_new(dev, size, 0, TTM_PL_FLAG_VRAM, |
Ben Skeggs | d550c41 | 2011-02-16 08:41:56 +1000 | [diff] [blame] | 576 | 0, 0, &dev_priv->fence.bo); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 577 | if (ret) |
| 578 | return ret; |
| 579 | |
| 580 | ret = nouveau_bo_pin(dev_priv->fence.bo, TTM_PL_FLAG_VRAM); |
| 581 | if (ret) |
| 582 | goto fail; |
| 583 | |
| 584 | ret = nouveau_bo_map(dev_priv->fence.bo); |
| 585 | if (ret) |
| 586 | goto fail; |
| 587 | |
| 588 | ret = drm_mm_init(&dev_priv->fence.heap, 0, |
| 589 | dev_priv->fence.bo->bo.mem.size); |
| 590 | if (ret) |
| 591 | goto fail; |
| 592 | |
| 593 | spin_lock_init(&dev_priv->fence.lock); |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | fail: |
| 598 | nouveau_bo_unmap(dev_priv->fence.bo); |
| 599 | nouveau_bo_ref(NULL, &dev_priv->fence.bo); |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | nouveau_fence_fini(struct drm_device *dev) |
| 605 | { |
| 606 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 607 | |
| 608 | if (USE_SEMA(dev)) { |
| 609 | drm_mm_takedown(&dev_priv->fence.heap); |
| 610 | nouveau_bo_unmap(dev_priv->fence.bo); |
| 611 | nouveau_bo_unpin(dev_priv->fence.bo); |
| 612 | nouveau_bo_ref(NULL, &dev_priv->fence.bo); |
| 613 | } |
| 614 | } |