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 <linux/ktime.h> 28 #include <linux/hrtimer.h> 29 #include <linux/sched/signal.h> 30 #include <trace/events/dma_fence.h> 31 32 #include <nvif/if0020.h> 33 34 #include "nouveau_drv.h" 35 #include "nouveau_dma.h" 36 #include "nouveau_fence.h" 37 38 static const struct dma_fence_ops nouveau_fence_ops_uevent; 39 static const struct dma_fence_ops nouveau_fence_ops_legacy; 40 41 static inline struct nouveau_fence * 42 from_fence(struct dma_fence *fence) 43 { 44 return container_of(fence, struct nouveau_fence, base); 45 } 46 47 static inline struct nouveau_fence_chan * 48 nouveau_fctx(struct nouveau_fence *fence) 49 { 50 return container_of(fence->base.lock, struct nouveau_fence_chan, lock); 51 } 52 53 static int 54 nouveau_fence_signal(struct nouveau_fence *fence) 55 { 56 int drop = 0; 57 58 dma_fence_signal_locked(&fence->base); 59 list_del(&fence->head); 60 rcu_assign_pointer(fence->channel, NULL); 61 62 if (test_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags)) { 63 struct nouveau_fence_chan *fctx = nouveau_fctx(fence); 64 65 if (!--fctx->notify_ref) 66 drop = 1; 67 } 68 69 dma_fence_put(&fence->base); 70 return drop; 71 } 72 73 static struct nouveau_fence * 74 nouveau_local_fence(struct dma_fence *fence, struct nouveau_drm *drm) 75 { 76 if (fence->ops != &nouveau_fence_ops_legacy && 77 fence->ops != &nouveau_fence_ops_uevent) 78 return NULL; 79 80 return from_fence(fence); 81 } 82 83 void 84 nouveau_fence_context_kill(struct nouveau_fence_chan *fctx, int error) 85 { 86 struct nouveau_fence *fence; 87 unsigned long flags; 88 89 spin_lock_irqsave(&fctx->lock, flags); 90 while (!list_empty(&fctx->pending)) { 91 fence = list_entry(fctx->pending.next, typeof(*fence), head); 92 93 if (error) 94 dma_fence_set_error(&fence->base, error); 95 96 if (nouveau_fence_signal(fence)) 97 nvif_event_block(&fctx->event); 98 } 99 fctx->killed = 1; 100 spin_unlock_irqrestore(&fctx->lock, flags); 101 } 102 103 void 104 nouveau_fence_context_del(struct nouveau_fence_chan *fctx) 105 { 106 cancel_work_sync(&fctx->uevent_work); 107 nouveau_fence_context_kill(fctx, 0); 108 nvif_event_dtor(&fctx->event); 109 fctx->dead = 1; 110 111 /* 112 * Ensure that all accesses to fence->channel complete before freeing 113 * the channel. 114 */ 115 synchronize_rcu(); 116 } 117 118 static void 119 nouveau_fence_context_put(struct kref *fence_ref) 120 { 121 kfree(container_of(fence_ref, struct nouveau_fence_chan, fence_ref)); 122 } 123 124 void 125 nouveau_fence_context_free(struct nouveau_fence_chan *fctx) 126 { 127 kref_put(&fctx->fence_ref, nouveau_fence_context_put); 128 } 129 130 static int 131 nouveau_fence_update(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx) 132 { 133 struct nouveau_fence *fence; 134 int drop = 0; 135 u32 seq = fctx->read(chan); 136 137 while (!list_empty(&fctx->pending)) { 138 fence = list_entry(fctx->pending.next, typeof(*fence), head); 139 140 if ((int)(seq - fence->base.seqno) < 0) 141 break; 142 143 drop |= nouveau_fence_signal(fence); 144 } 145 146 return drop; 147 } 148 149 static void 150 nouveau_fence_uevent_work(struct work_struct *work) 151 { 152 struct nouveau_fence_chan *fctx = container_of(work, struct nouveau_fence_chan, 153 uevent_work); 154 unsigned long flags; 155 int drop = 0; 156 157 spin_lock_irqsave(&fctx->lock, flags); 158 if (!list_empty(&fctx->pending)) { 159 struct nouveau_fence *fence; 160 struct nouveau_channel *chan; 161 162 fence = list_entry(fctx->pending.next, typeof(*fence), head); 163 chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock)); 164 if (nouveau_fence_update(chan, fctx)) 165 drop = 1; 166 } 167 if (drop) 168 nvif_event_block(&fctx->event); 169 170 spin_unlock_irqrestore(&fctx->lock, flags); 171 } 172 173 static int 174 nouveau_fence_wait_uevent_handler(struct nvif_event *event, void *repv, u32 repc) 175 { 176 struct nouveau_fence_chan *fctx = container_of(event, typeof(*fctx), event); 177 schedule_work(&fctx->uevent_work); 178 return NVIF_EVENT_KEEP; 179 } 180 181 void 182 nouveau_fence_context_new(struct nouveau_channel *chan, struct nouveau_fence_chan *fctx) 183 { 184 struct nouveau_cli *cli = chan->cli; 185 struct nouveau_drm *drm = cli->drm; 186 struct nouveau_fence_priv *priv = (void*)drm->fence; 187 struct { 188 struct nvif_event_v0 base; 189 struct nvif_chan_event_v0 host; 190 } args; 191 int ret; 192 193 INIT_WORK(&fctx->uevent_work, nouveau_fence_uevent_work); 194 INIT_LIST_HEAD(&fctx->flip); 195 INIT_LIST_HEAD(&fctx->pending); 196 spin_lock_init(&fctx->lock); 197 fctx->context = drm->runl[chan->runlist].context_base + chan->chid; 198 199 if (chan == drm->cechan) 200 strcpy(fctx->name, "copy engine channel"); 201 else if (chan == drm->channel) 202 strcpy(fctx->name, "generic kernel channel"); 203 else 204 strcpy(fctx->name, cli->name); 205 206 kref_init(&fctx->fence_ref); 207 if (!priv->uevent) 208 return; 209 210 args.host.version = 0; 211 args.host.type = NVIF_CHAN_EVENT_V0_NON_STALL_INTR; 212 213 ret = nvif_event_ctor(&chan->user, "fenceNonStallIntr", (chan->runlist << 16) | chan->chid, 214 nouveau_fence_wait_uevent_handler, false, 215 &args.base, sizeof(args), &fctx->event); 216 217 WARN_ON(ret); 218 } 219 220 int 221 nouveau_fence_emit(struct nouveau_fence *fence) 222 { 223 struct nouveau_channel *chan = unrcu_pointer(fence->channel); 224 struct nouveau_fence_chan *fctx = chan->fence; 225 struct nouveau_fence_priv *priv = (void*)chan->cli->drm->fence; 226 int ret; 227 228 fence->timeout = jiffies + (15 * HZ); 229 230 if (priv->uevent) 231 dma_fence_init(&fence->base, &nouveau_fence_ops_uevent, 232 &fctx->lock, fctx->context, ++fctx->sequence); 233 else 234 dma_fence_init(&fence->base, &nouveau_fence_ops_legacy, 235 &fctx->lock, fctx->context, ++fctx->sequence); 236 kref_get(&fctx->fence_ref); 237 238 ret = fctx->emit(fence); 239 if (!ret) { 240 dma_fence_get(&fence->base); 241 spin_lock_irq(&fctx->lock); 242 243 if (unlikely(fctx->killed)) { 244 spin_unlock_irq(&fctx->lock); 245 dma_fence_put(&fence->base); 246 return -ENODEV; 247 } 248 249 if (nouveau_fence_update(chan, fctx)) 250 nvif_event_block(&fctx->event); 251 252 list_add_tail(&fence->head, &fctx->pending); 253 spin_unlock_irq(&fctx->lock); 254 } 255 256 return ret; 257 } 258 259 bool 260 nouveau_fence_done(struct nouveau_fence *fence) 261 { 262 if (fence->base.ops == &nouveau_fence_ops_legacy || 263 fence->base.ops == &nouveau_fence_ops_uevent) { 264 struct nouveau_fence_chan *fctx = nouveau_fctx(fence); 265 struct nouveau_channel *chan; 266 unsigned long flags; 267 268 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags)) 269 return true; 270 271 spin_lock_irqsave(&fctx->lock, flags); 272 chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock)); 273 if (chan && nouveau_fence_update(chan, fctx)) 274 nvif_event_block(&fctx->event); 275 spin_unlock_irqrestore(&fctx->lock, flags); 276 } 277 return dma_fence_is_signaled(&fence->base); 278 } 279 280 static long 281 nouveau_fence_wait_legacy(struct dma_fence *f, bool intr, long wait) 282 { 283 struct nouveau_fence *fence = from_fence(f); 284 unsigned long sleep_time = NSEC_PER_MSEC / 1000; 285 unsigned long t = jiffies, timeout = t + wait; 286 287 while (!nouveau_fence_done(fence)) { 288 ktime_t kt; 289 290 t = jiffies; 291 292 if (wait != MAX_SCHEDULE_TIMEOUT && time_after_eq(t, timeout)) { 293 __set_current_state(TASK_RUNNING); 294 return 0; 295 } 296 297 __set_current_state(intr ? TASK_INTERRUPTIBLE : 298 TASK_UNINTERRUPTIBLE); 299 300 kt = sleep_time; 301 schedule_hrtimeout(&kt, HRTIMER_MODE_REL); 302 sleep_time *= 2; 303 if (sleep_time > NSEC_PER_MSEC) 304 sleep_time = NSEC_PER_MSEC; 305 306 if (intr && signal_pending(current)) 307 return -ERESTARTSYS; 308 } 309 310 __set_current_state(TASK_RUNNING); 311 312 return timeout - t; 313 } 314 315 static int 316 nouveau_fence_wait_busy(struct nouveau_fence *fence, bool intr) 317 { 318 int ret = 0; 319 320 while (!nouveau_fence_done(fence)) { 321 if (time_after_eq(jiffies, fence->timeout)) { 322 ret = -EBUSY; 323 break; 324 } 325 326 __set_current_state(intr ? 327 TASK_INTERRUPTIBLE : 328 TASK_UNINTERRUPTIBLE); 329 330 if (intr && signal_pending(current)) { 331 ret = -ERESTARTSYS; 332 break; 333 } 334 } 335 336 __set_current_state(TASK_RUNNING); 337 return ret; 338 } 339 340 int 341 nouveau_fence_wait(struct nouveau_fence *fence, bool lazy, bool intr) 342 { 343 long ret; 344 345 if (!lazy) 346 return nouveau_fence_wait_busy(fence, intr); 347 348 ret = dma_fence_wait_timeout(&fence->base, intr, 15 * HZ); 349 if (ret < 0) 350 return ret; 351 else if (!ret) 352 return -EBUSY; 353 else 354 return 0; 355 } 356 357 int 358 nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, 359 bool exclusive, bool intr) 360 { 361 struct nouveau_fence_chan *fctx = chan->fence; 362 struct dma_resv *resv = nvbo->bo.base.resv; 363 int i, ret; 364 365 ret = dma_resv_reserve_fences(resv, 1); 366 if (ret) 367 return ret; 368 369 /* Waiting for the writes first causes performance regressions 370 * under some circumstances. So manually wait for the reads first. 371 */ 372 for (i = 0; i < 2; ++i) { 373 struct dma_resv_iter cursor; 374 struct dma_fence *fence; 375 376 dma_resv_for_each_fence(&cursor, resv, 377 dma_resv_usage_rw(exclusive), 378 fence) { 379 enum dma_resv_usage usage; 380 struct nouveau_fence *f; 381 382 usage = dma_resv_iter_usage(&cursor); 383 if (i == 0 && usage == DMA_RESV_USAGE_WRITE) 384 continue; 385 386 f = nouveau_local_fence(fence, chan->cli->drm); 387 if (f) { 388 struct nouveau_channel *prev; 389 bool must_wait = true; 390 391 rcu_read_lock(); 392 prev = rcu_dereference(f->channel); 393 if (prev && (prev == chan || 394 fctx->sync(f, prev, chan) == 0)) 395 must_wait = false; 396 rcu_read_unlock(); 397 if (!must_wait) 398 continue; 399 } 400 401 ret = dma_fence_wait(fence, intr); 402 if (ret) 403 return ret; 404 } 405 } 406 407 return 0; 408 } 409 410 void 411 nouveau_fence_unref(struct nouveau_fence **pfence) 412 { 413 if (*pfence) 414 dma_fence_put(&(*pfence)->base); 415 *pfence = NULL; 416 } 417 418 int 419 nouveau_fence_create(struct nouveau_fence **pfence, 420 struct nouveau_channel *chan) 421 { 422 struct nouveau_fence *fence; 423 424 if (unlikely(!chan->fence)) 425 return -ENODEV; 426 427 fence = kzalloc(sizeof(*fence), GFP_KERNEL); 428 if (!fence) 429 return -ENOMEM; 430 431 fence->channel = chan; 432 433 *pfence = fence; 434 return 0; 435 } 436 437 int 438 nouveau_fence_new(struct nouveau_fence **pfence, 439 struct nouveau_channel *chan) 440 { 441 int ret = 0; 442 443 ret = nouveau_fence_create(pfence, chan); 444 if (ret) 445 return ret; 446 447 ret = nouveau_fence_emit(*pfence); 448 if (ret) 449 nouveau_fence_unref(pfence); 450 451 return ret; 452 } 453 454 static const char *nouveau_fence_get_get_driver_name(struct dma_fence *fence) 455 { 456 return "nouveau"; 457 } 458 459 static const char *nouveau_fence_get_timeline_name(struct dma_fence *f) 460 { 461 struct nouveau_fence *fence = from_fence(f); 462 struct nouveau_fence_chan *fctx = nouveau_fctx(fence); 463 464 return !fctx->dead ? fctx->name : "dead channel"; 465 } 466 467 /* 468 * In an ideal world, read would not assume the channel context is still alive. 469 * This function may be called from another device, running into free memory as a 470 * result. The drm node should still be there, so we can derive the index from 471 * the fence context. 472 */ 473 static bool nouveau_fence_is_signaled(struct dma_fence *f) 474 { 475 struct nouveau_fence *fence = from_fence(f); 476 struct nouveau_fence_chan *fctx = nouveau_fctx(fence); 477 struct nouveau_channel *chan; 478 bool ret = false; 479 480 rcu_read_lock(); 481 chan = rcu_dereference(fence->channel); 482 if (chan) 483 ret = (int)(fctx->read(chan) - fence->base.seqno) >= 0; 484 rcu_read_unlock(); 485 486 return ret; 487 } 488 489 static bool nouveau_fence_no_signaling(struct dma_fence *f) 490 { 491 struct nouveau_fence *fence = from_fence(f); 492 493 /* 494 * caller should have a reference on the fence, 495 * else fence could get freed here 496 */ 497 WARN_ON(kref_read(&fence->base.refcount) <= 1); 498 499 /* 500 * This needs uevents to work correctly, but dma_fence_add_callback relies on 501 * being able to enable signaling. It will still get signaled eventually, 502 * just not right away. 503 */ 504 if (nouveau_fence_is_signaled(f)) { 505 list_del(&fence->head); 506 507 dma_fence_put(&fence->base); 508 return false; 509 } 510 511 return true; 512 } 513 514 static void nouveau_fence_release(struct dma_fence *f) 515 { 516 struct nouveau_fence *fence = from_fence(f); 517 struct nouveau_fence_chan *fctx = nouveau_fctx(fence); 518 519 kref_put(&fctx->fence_ref, nouveau_fence_context_put); 520 dma_fence_free(&fence->base); 521 } 522 523 static const struct dma_fence_ops nouveau_fence_ops_legacy = { 524 .get_driver_name = nouveau_fence_get_get_driver_name, 525 .get_timeline_name = nouveau_fence_get_timeline_name, 526 .enable_signaling = nouveau_fence_no_signaling, 527 .signaled = nouveau_fence_is_signaled, 528 .wait = nouveau_fence_wait_legacy, 529 .release = nouveau_fence_release 530 }; 531 532 static bool nouveau_fence_enable_signaling(struct dma_fence *f) 533 { 534 struct nouveau_fence *fence = from_fence(f); 535 struct nouveau_fence_chan *fctx = nouveau_fctx(fence); 536 bool ret; 537 538 if (!fctx->notify_ref++) 539 nvif_event_allow(&fctx->event); 540 541 ret = nouveau_fence_no_signaling(f); 542 if (ret) 543 set_bit(DMA_FENCE_FLAG_USER_BITS, &fence->base.flags); 544 else if (!--fctx->notify_ref) 545 nvif_event_block(&fctx->event); 546 547 return ret; 548 } 549 550 static const struct dma_fence_ops nouveau_fence_ops_uevent = { 551 .get_driver_name = nouveau_fence_get_get_driver_name, 552 .get_timeline_name = nouveau_fence_get_timeline_name, 553 .enable_signaling = nouveau_fence_enable_signaling, 554 .signaled = nouveau_fence_is_signaled, 555 .release = nouveau_fence_release 556 }; 557