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 | |
| 30 | #include "nouveau_drv.h" |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 31 | #include "nouveau_ramht.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 32 | #include "nouveau_dma.h" |
| 33 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 34 | #define USE_REFCNT(dev) (nouveau_private(dev)->chipset >= 0x10) |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 35 | #define USE_SEMA(dev) (nouveau_private(dev)->chipset >= 0x17) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 36 | |
| 37 | struct nouveau_fence { |
| 38 | struct nouveau_channel *channel; |
| 39 | struct kref refcount; |
| 40 | struct list_head entry; |
| 41 | |
| 42 | uint32_t sequence; |
| 43 | bool signalled; |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 44 | |
| 45 | void (*work)(void *priv, bool signalled); |
| 46 | void *priv; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 47 | }; |
| 48 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 49 | struct nouveau_semaphore { |
| 50 | struct kref ref; |
| 51 | struct drm_device *dev; |
| 52 | struct drm_mm_node *mem; |
| 53 | }; |
| 54 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 55 | static inline struct nouveau_fence * |
| 56 | nouveau_fence(void *sync_obj) |
| 57 | { |
| 58 | return (struct nouveau_fence *)sync_obj; |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | nouveau_fence_del(struct kref *ref) |
| 63 | { |
| 64 | struct nouveau_fence *fence = |
| 65 | container_of(ref, struct nouveau_fence, refcount); |
| 66 | |
Francisco Jerez | 2a6789a | 2010-10-18 03:56:14 +0200 | [diff] [blame] | 67 | nouveau_channel_ref(NULL, &fence->channel); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 68 | kfree(fence); |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | nouveau_fence_update(struct nouveau_channel *chan) |
| 73 | { |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 74 | struct drm_device *dev = chan->dev; |
| 75 | struct nouveau_fence *tmp, *fence; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 76 | uint32_t sequence; |
| 77 | |
Francisco Jerez | 3ba6462 | 2010-08-28 17:56:33 +0200 | [diff] [blame] | 78 | spin_lock(&chan->fence.lock); |
| 79 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 80 | if (USE_REFCNT(dev)) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 81 | sequence = nvchan_rd32(chan, 0x48); |
| 82 | else |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 83 | sequence = atomic_read(&chan->fence.last_sequence_irq); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 84 | |
| 85 | if (chan->fence.sequence_ack == sequence) |
Francisco Jerez | 3ba6462 | 2010-08-28 17:56:33 +0200 | [diff] [blame] | 86 | goto out; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 87 | chan->fence.sequence_ack = sequence; |
| 88 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 89 | list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 90 | sequence = fence->sequence; |
| 91 | fence->signalled = true; |
| 92 | list_del(&fence->entry); |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 93 | |
| 94 | if (unlikely(fence->work)) |
| 95 | fence->work(fence->priv, true); |
| 96 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 97 | kref_put(&fence->refcount, nouveau_fence_del); |
| 98 | |
| 99 | if (sequence == chan->fence.sequence_ack) |
| 100 | break; |
| 101 | } |
Francisco Jerez | 3ba6462 | 2010-08-28 17:56:33 +0200 | [diff] [blame] | 102 | out: |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 103 | spin_unlock(&chan->fence.lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int |
| 107 | nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **pfence, |
| 108 | bool emit) |
| 109 | { |
| 110 | struct nouveau_fence *fence; |
| 111 | int ret = 0; |
| 112 | |
| 113 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
| 114 | if (!fence) |
| 115 | return -ENOMEM; |
| 116 | kref_init(&fence->refcount); |
Francisco Jerez | 2a6789a | 2010-10-18 03:56:14 +0200 | [diff] [blame] | 117 | nouveau_channel_ref(chan, &fence->channel); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 118 | |
| 119 | if (emit) |
| 120 | ret = nouveau_fence_emit(fence); |
| 121 | |
| 122 | if (ret) |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 123 | nouveau_fence_unref(&fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 124 | *pfence = fence; |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | struct nouveau_channel * |
| 129 | nouveau_fence_channel(struct nouveau_fence *fence) |
| 130 | { |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 131 | return fence ? nouveau_channel_get_unlocked(fence->channel) : NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | int |
| 135 | nouveau_fence_emit(struct nouveau_fence *fence) |
| 136 | { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 137 | struct nouveau_channel *chan = fence->channel; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 138 | struct drm_device *dev = chan->dev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 139 | int ret; |
| 140 | |
| 141 | ret = RING_SPACE(chan, 2); |
| 142 | if (ret) |
| 143 | return ret; |
| 144 | |
| 145 | if (unlikely(chan->fence.sequence == chan->fence.sequence_ack - 1)) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 146 | nouveau_fence_update(chan); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 147 | |
| 148 | BUG_ON(chan->fence.sequence == |
| 149 | chan->fence.sequence_ack - 1); |
| 150 | } |
| 151 | |
| 152 | fence->sequence = ++chan->fence.sequence; |
| 153 | |
| 154 | kref_get(&fence->refcount); |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 155 | spin_lock(&chan->fence.lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 156 | list_add_tail(&fence->entry, &chan->fence.pending); |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 157 | spin_unlock(&chan->fence.lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 158 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 159 | BEGIN_RING(chan, NvSubSw, USE_REFCNT(dev) ? 0x0050 : 0x0150, 1); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 160 | OUT_RING(chan, fence->sequence); |
| 161 | FIRE_RING(chan); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | void |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 167 | nouveau_fence_work(struct nouveau_fence *fence, |
| 168 | void (*work)(void *priv, bool signalled), |
| 169 | void *priv) |
| 170 | { |
| 171 | BUG_ON(fence->work); |
| 172 | |
| 173 | spin_lock(&fence->channel->fence.lock); |
| 174 | |
| 175 | if (fence->signalled) { |
| 176 | work(priv, true); |
| 177 | } else { |
| 178 | fence->work = work; |
| 179 | fence->priv = priv; |
| 180 | } |
| 181 | |
| 182 | spin_unlock(&fence->channel->fence.lock); |
| 183 | } |
| 184 | |
| 185 | void |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 186 | __nouveau_fence_unref(void **sync_obj) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 187 | { |
| 188 | struct nouveau_fence *fence = nouveau_fence(*sync_obj); |
| 189 | |
| 190 | if (fence) |
| 191 | kref_put(&fence->refcount, nouveau_fence_del); |
| 192 | *sync_obj = NULL; |
| 193 | } |
| 194 | |
| 195 | void * |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 196 | __nouveau_fence_ref(void *sync_obj) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 197 | { |
| 198 | struct nouveau_fence *fence = nouveau_fence(sync_obj); |
| 199 | |
| 200 | kref_get(&fence->refcount); |
| 201 | return sync_obj; |
| 202 | } |
| 203 | |
| 204 | bool |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 205 | __nouveau_fence_signalled(void *sync_obj, void *sync_arg) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 206 | { |
| 207 | struct nouveau_fence *fence = nouveau_fence(sync_obj); |
| 208 | struct nouveau_channel *chan = fence->channel; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 209 | |
| 210 | if (fence->signalled) |
| 211 | return true; |
| 212 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 213 | nouveau_fence_update(chan); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 214 | return fence->signalled; |
| 215 | } |
| 216 | |
| 217 | int |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 218 | __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] | 219 | { |
| 220 | unsigned long timeout = jiffies + (3 * DRM_HZ); |
| 221 | int ret = 0; |
| 222 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 223 | while (1) { |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 224 | if (__nouveau_fence_signalled(sync_obj, sync_arg)) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 225 | break; |
| 226 | |
| 227 | if (time_after_eq(jiffies, timeout)) { |
| 228 | ret = -EBUSY; |
| 229 | break; |
| 230 | } |
| 231 | |
Kulikov Vasiliy | 0599111 | 2010-07-26 12:23:54 +0400 | [diff] [blame] | 232 | __set_current_state(intr ? TASK_INTERRUPTIBLE |
| 233 | : TASK_UNINTERRUPTIBLE); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 234 | if (lazy) |
| 235 | schedule_timeout(1); |
| 236 | |
| 237 | if (intr && signal_pending(current)) { |
Ben Skeggs | 9ddc8c5 | 2009-12-15 11:04:25 +1000 | [diff] [blame] | 238 | ret = -ERESTARTSYS; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | __set_current_state(TASK_RUNNING); |
| 244 | |
| 245 | return ret; |
| 246 | } |
| 247 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 248 | static struct nouveau_semaphore * |
| 249 | alloc_semaphore(struct drm_device *dev) |
| 250 | { |
| 251 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 252 | struct nouveau_semaphore *sema; |
Francisco Jerez | 907af60 | 2010-10-09 04:02:09 +0200 | [diff] [blame] | 253 | int ret; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 254 | |
| 255 | if (!USE_SEMA(dev)) |
| 256 | return NULL; |
| 257 | |
| 258 | sema = kmalloc(sizeof(*sema), GFP_KERNEL); |
| 259 | if (!sema) |
| 260 | goto fail; |
| 261 | |
Francisco Jerez | 907af60 | 2010-10-09 04:02:09 +0200 | [diff] [blame] | 262 | ret = drm_mm_pre_get(&dev_priv->fence.heap); |
| 263 | if (ret) |
| 264 | goto fail; |
| 265 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 266 | spin_lock(&dev_priv->fence.lock); |
| 267 | sema->mem = drm_mm_search_free(&dev_priv->fence.heap, 4, 0, 0); |
| 268 | if (sema->mem) |
Francisco Jerez | 907af60 | 2010-10-09 04:02:09 +0200 | [diff] [blame] | 269 | sema->mem = drm_mm_get_block_atomic(sema->mem, 4, 0); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 270 | spin_unlock(&dev_priv->fence.lock); |
| 271 | |
| 272 | if (!sema->mem) |
| 273 | goto fail; |
| 274 | |
| 275 | kref_init(&sema->ref); |
| 276 | sema->dev = dev; |
| 277 | nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 0); |
| 278 | |
| 279 | return sema; |
| 280 | fail: |
| 281 | kfree(sema); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | free_semaphore(struct kref *ref) |
| 287 | { |
| 288 | struct nouveau_semaphore *sema = |
| 289 | container_of(ref, struct nouveau_semaphore, ref); |
| 290 | struct drm_nouveau_private *dev_priv = sema->dev->dev_private; |
| 291 | |
| 292 | spin_lock(&dev_priv->fence.lock); |
| 293 | drm_mm_put_block(sema->mem); |
| 294 | spin_unlock(&dev_priv->fence.lock); |
| 295 | |
| 296 | kfree(sema); |
| 297 | } |
| 298 | |
| 299 | static void |
| 300 | semaphore_work(void *priv, bool signalled) |
| 301 | { |
| 302 | struct nouveau_semaphore *sema = priv; |
| 303 | struct drm_nouveau_private *dev_priv = sema->dev->dev_private; |
| 304 | |
| 305 | if (unlikely(!signalled)) |
| 306 | nouveau_bo_wr32(dev_priv->fence.bo, sema->mem->start / 4, 1); |
| 307 | |
| 308 | kref_put(&sema->ref, free_semaphore); |
| 309 | } |
| 310 | |
| 311 | static int |
| 312 | emit_semaphore(struct nouveau_channel *chan, int method, |
| 313 | struct nouveau_semaphore *sema) |
| 314 | { |
| 315 | struct drm_nouveau_private *dev_priv = sema->dev->dev_private; |
| 316 | struct nouveau_fence *fence; |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 317 | bool smart = (dev_priv->card_type >= NV_50); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 318 | int ret; |
| 319 | |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 320 | ret = RING_SPACE(chan, smart ? 8 : 4); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 321 | if (ret) |
| 322 | return ret; |
| 323 | |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 324 | if (smart) { |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 325 | BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1); |
| 326 | OUT_RING(chan, NvSema); |
| 327 | } |
| 328 | BEGIN_RING(chan, NvSubSw, NV_SW_SEMAPHORE_OFFSET, 1); |
| 329 | OUT_RING(chan, sema->mem->start); |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 330 | |
| 331 | if (smart && method == NV_SW_SEMAPHORE_ACQUIRE) { |
| 332 | /* |
| 333 | * NV50 tries to be too smart and context-switch |
| 334 | * between semaphores instead of doing a "first come, |
| 335 | * first served" strategy like previous cards |
| 336 | * do. |
| 337 | * |
| 338 | * That's bad because the ACQUIRE latency can get as |
| 339 | * large as the PFIFO context time slice in the |
| 340 | * typical DRI2 case where you have several |
| 341 | * outstanding semaphores at the same moment. |
| 342 | * |
| 343 | * If we're going to ACQUIRE, force the card to |
| 344 | * context switch before, just in case the matching |
| 345 | * RELEASE is already scheduled to be executed in |
| 346 | * another channel. |
| 347 | */ |
| 348 | BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1); |
| 349 | OUT_RING(chan, 0); |
| 350 | } |
| 351 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 352 | BEGIN_RING(chan, NvSubSw, method, 1); |
| 353 | OUT_RING(chan, 1); |
| 354 | |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 355 | if (smart && method == NV_SW_SEMAPHORE_RELEASE) { |
| 356 | /* |
| 357 | * Force the card to context switch, there may be |
| 358 | * another channel waiting for the semaphore we just |
| 359 | * released. |
| 360 | */ |
| 361 | BEGIN_RING(chan, NvSubSw, NV_SW_YIELD, 1); |
| 362 | OUT_RING(chan, 0); |
| 363 | } |
| 364 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 365 | /* Delay semaphore destruction until its work is done */ |
| 366 | ret = nouveau_fence_new(chan, &fence, true); |
| 367 | if (ret) |
| 368 | return ret; |
| 369 | |
| 370 | kref_get(&sema->ref); |
| 371 | nouveau_fence_work(fence, semaphore_work, sema); |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 372 | nouveau_fence_unref(&fence); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 377 | int |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 378 | nouveau_fence_sync(struct nouveau_fence *fence, |
| 379 | struct nouveau_channel *wchan) |
| 380 | { |
| 381 | struct nouveau_channel *chan = nouveau_fence_channel(fence); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 382 | struct drm_device *dev = wchan->dev; |
| 383 | struct nouveau_semaphore *sema; |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 384 | int ret = 0; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 385 | |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 386 | if (likely(!chan || chan == wchan || |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 387 | nouveau_fence_signalled(fence))) |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 388 | goto out; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 389 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 390 | sema = alloc_semaphore(dev); |
| 391 | if (!sema) { |
| 392 | /* Early card or broken userspace, fall back to |
| 393 | * software sync. */ |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 394 | ret = nouveau_fence_wait(fence, true, false); |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 395 | goto out; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Ben Skeggs | 08cd3d4 | 2010-10-12 08:01:59 +1000 | [diff] [blame] | 398 | /* 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] | 399 | * we have to fallback to software sync to prevent locking |
| 400 | * order issues |
| 401 | */ |
Ben Skeggs | 08cd3d4 | 2010-10-12 08:01:59 +1000 | [diff] [blame] | 402 | if (!mutex_trylock(&chan->mutex)) { |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 403 | ret = nouveau_fence_wait(fence, true, false); |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 404 | goto out_unref; |
Ben Skeggs | cff5c13 | 2010-10-06 16:16:59 +1000 | [diff] [blame] | 405 | } |
| 406 | |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 407 | /* Make wchan wait until it gets signalled */ |
| 408 | ret = emit_semaphore(wchan, NV_SW_SEMAPHORE_ACQUIRE, sema); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 409 | if (ret) |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 410 | goto out_unlock; |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 411 | |
Francisco Jerez | 8af29cc | 2010-10-02 17:04:46 +0200 | [diff] [blame] | 412 | /* Signal the semaphore from chan */ |
| 413 | ret = emit_semaphore(chan, NV_SW_SEMAPHORE_RELEASE, sema); |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 414 | |
| 415 | out_unlock: |
Ben Skeggs | 08cd3d4 | 2010-10-12 08:01:59 +1000 | [diff] [blame] | 416 | mutex_unlock(&chan->mutex); |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 417 | out_unref: |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 418 | kref_put(&sema->ref, free_semaphore); |
Francisco Jerez | 2b478ad | 2010-10-18 03:56:40 +0200 | [diff] [blame] | 419 | out: |
| 420 | if (chan) |
| 421 | nouveau_channel_put_unlocked(&chan); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 422 | return ret; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | int |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 426 | __nouveau_fence_flush(void *sync_obj, void *sync_arg) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 427 | { |
| 428 | return 0; |
| 429 | } |
| 430 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 431 | int |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 432 | nouveau_fence_channel_init(struct nouveau_channel *chan) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 433 | { |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 434 | struct drm_device *dev = chan->dev; |
| 435 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 436 | struct nouveau_gpuobj *obj = NULL; |
| 437 | int ret; |
| 438 | |
| 439 | /* Create an NV_SW object for various sync purposes */ |
Ben Skeggs | a6a1a38 | 2010-10-19 19:57:34 +1000 | [diff] [blame] | 440 | ret = nouveau_gpuobj_gr_new(chan, NV_SW, &obj); |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 441 | if (ret) |
| 442 | return ret; |
| 443 | |
| 444 | ret = nouveau_ramht_insert(chan, NvSw, obj); |
| 445 | nouveau_gpuobj_ref(NULL, &obj); |
| 446 | if (ret) |
| 447 | return ret; |
| 448 | |
| 449 | ret = RING_SPACE(chan, 2); |
| 450 | if (ret) |
| 451 | return ret; |
| 452 | BEGIN_RING(chan, NvSubSw, 0, 1); |
| 453 | OUT_RING(chan, NvSw); |
| 454 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 455 | /* Create a DMA object for the shared cross-channel sync area. */ |
| 456 | if (USE_SEMA(dev)) { |
| 457 | struct drm_mm_node *mem = dev_priv->fence.bo->bo.mem.mm_node; |
| 458 | |
| 459 | ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY, |
| 460 | mem->start << PAGE_SHIFT, |
| 461 | mem->size << PAGE_SHIFT, |
Ben Skeggs | 7f4a195 | 2010-11-16 11:50:09 +1000 | [diff] [blame^] | 462 | NV_MEM_ACCESS_RW, |
| 463 | NV_MEM_TARGET_VRAM, &obj); |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 464 | if (ret) |
| 465 | return ret; |
| 466 | |
| 467 | ret = nouveau_ramht_insert(chan, NvSema, obj); |
| 468 | nouveau_gpuobj_ref(NULL, &obj); |
| 469 | if (ret) |
| 470 | return ret; |
| 471 | |
| 472 | ret = RING_SPACE(chan, 2); |
| 473 | if (ret) |
| 474 | return ret; |
| 475 | BEGIN_RING(chan, NvSubSw, NV_SW_DMA_SEMAPHORE, 1); |
| 476 | OUT_RING(chan, NvSema); |
| 477 | } |
| 478 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 479 | FIRE_RING(chan); |
| 480 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 481 | INIT_LIST_HEAD(&chan->fence.pending); |
| 482 | spin_lock_init(&chan->fence.lock); |
Ben Skeggs | 047d1d3 | 2010-05-31 12:00:43 +1000 | [diff] [blame] | 483 | atomic_set(&chan->fence.last_sequence_irq, 0); |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 484 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | void |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 489 | nouveau_fence_channel_fini(struct nouveau_channel *chan) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 490 | { |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 491 | struct nouveau_fence *tmp, *fence; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 492 | |
Francisco Jerez | 889fa93 | 2010-10-18 03:57:19 +0200 | [diff] [blame] | 493 | spin_lock(&chan->fence.lock); |
| 494 | |
Francisco Jerez | 2730723 | 2010-09-21 18:57:11 +0200 | [diff] [blame] | 495 | list_for_each_entry_safe(fence, tmp, &chan->fence.pending, entry) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 496 | fence->signalled = true; |
| 497 | list_del(&fence->entry); |
Francisco Jerez | 8ac3891 | 2010-09-21 20:49:39 +0200 | [diff] [blame] | 498 | |
| 499 | if (unlikely(fence->work)) |
| 500 | fence->work(fence->priv, false); |
| 501 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 502 | kref_put(&fence->refcount, nouveau_fence_del); |
| 503 | } |
Francisco Jerez | 889fa93 | 2010-10-18 03:57:19 +0200 | [diff] [blame] | 504 | |
| 505 | spin_unlock(&chan->fence.lock); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 506 | } |
| 507 | |
Francisco Jerez | 0c6c1c2 | 2010-09-22 00:58:54 +0200 | [diff] [blame] | 508 | int |
| 509 | nouveau_fence_init(struct drm_device *dev) |
| 510 | { |
| 511 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 512 | int ret; |
| 513 | |
| 514 | /* Create a shared VRAM heap for cross-channel sync. */ |
| 515 | if (USE_SEMA(dev)) { |
| 516 | ret = nouveau_bo_new(dev, NULL, 4096, 0, TTM_PL_FLAG_VRAM, |
| 517 | 0, 0, false, true, &dev_priv->fence.bo); |
| 518 | if (ret) |
| 519 | return ret; |
| 520 | |
| 521 | ret = nouveau_bo_pin(dev_priv->fence.bo, TTM_PL_FLAG_VRAM); |
| 522 | if (ret) |
| 523 | goto fail; |
| 524 | |
| 525 | ret = nouveau_bo_map(dev_priv->fence.bo); |
| 526 | if (ret) |
| 527 | goto fail; |
| 528 | |
| 529 | ret = drm_mm_init(&dev_priv->fence.heap, 0, |
| 530 | dev_priv->fence.bo->bo.mem.size); |
| 531 | if (ret) |
| 532 | goto fail; |
| 533 | |
| 534 | spin_lock_init(&dev_priv->fence.lock); |
| 535 | } |
| 536 | |
| 537 | return 0; |
| 538 | fail: |
| 539 | nouveau_bo_unmap(dev_priv->fence.bo); |
| 540 | nouveau_bo_ref(NULL, &dev_priv->fence.bo); |
| 541 | return ret; |
| 542 | } |
| 543 | |
| 544 | void |
| 545 | nouveau_fence_fini(struct drm_device *dev) |
| 546 | { |
| 547 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 548 | |
| 549 | if (USE_SEMA(dev)) { |
| 550 | drm_mm_takedown(&dev_priv->fence.heap); |
| 551 | nouveau_bo_unmap(dev_priv->fence.bo); |
| 552 | nouveau_bo_unpin(dev_priv->fence.bo); |
| 553 | nouveau_bo_ref(NULL, &dev_priv->fence.bo); |
| 554 | } |
| 555 | } |