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