-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathnvidia-drm-fence.c
More file actions
1842 lines (1512 loc) · 57.6 KB
/
nvidia-drm-fence.c
File metadata and controls
1842 lines (1512 loc) · 57.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016-2025, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "nvidia-drm-conftest.h"
#if defined(NV_DRM_AVAILABLE)
#if defined(NV_DRM_DRMP_H_PRESENT)
#include <drm/drmP.h>
#endif
#include "nvidia-drm-priv.h"
#include "nvidia-drm-gem.h"
#include "nvidia-drm-fence.h"
#include "nvidia-dma-resv-helper.h"
#include "nv_drm_common_ioctl.h"
#include <linux/dma-fence.h>
#define NV_DRM_SEMAPHORE_SURFACE_FENCE_MAX_TIMEOUT_MS 5000
struct nv_drm_fence_context;
struct nv_drm_fence_context_ops {
void (*destroy)(struct nv_drm_fence_context *nv_fence_context);
};
struct nv_drm_fence_context {
struct nv_drm_gem_object base;
const struct nv_drm_fence_context_ops *ops;
struct nv_drm_device *nv_dev;
uint64_t context;
NvU64 fenceSemIndex; /* Index into semaphore surface */
};
struct nv_drm_prime_fence_context {
struct nv_drm_fence_context base;
/* Mapped semaphore surface */
struct NvKmsKapiMemory *pSemSurface;
NvU32 *pLinearAddress;
/* Protects nv_drm_fence_context::{pending, last_seqno} */
spinlock_t lock;
/*
* Software signaling structures. __nv_drm_prime_fence_context_new()
* allocates channel event and __nv_drm_prime_fence_context_destroy() frees
* it. There are no simultaneous read/write access to 'cb', therefore it
* does not require spin-lock protection.
*/
struct NvKmsKapiChannelEvent *cb;
/* List of pending fences which are not yet signaled */
struct list_head pending;
unsigned last_seqno;
};
struct nv_drm_prime_fence {
struct list_head list_entry;
struct dma_fence base;
spinlock_t lock;
};
static inline
struct nv_drm_prime_fence *to_nv_drm_prime_fence(struct dma_fence *fence)
{
return container_of(fence, struct nv_drm_prime_fence, base);
}
static const char*
nv_drm_gem_fence_op_get_driver_name(struct dma_fence *fence)
{
return "NVIDIA";
}
static const char*
nv_drm_gem_prime_fence_op_get_timeline_name(struct dma_fence *fence)
{
return "nvidia.prime";
}
static bool nv_drm_gem_prime_fence_op_enable_signaling(struct dma_fence *fence)
{
// DO NOTHING
return true;
}
static void nv_drm_gem_prime_fence_op_release(struct dma_fence *fence)
{
struct nv_drm_prime_fence *nv_fence = to_nv_drm_prime_fence(fence);
nv_drm_free(nv_fence);
}
static signed long
nv_drm_gem_prime_fence_op_wait(struct dma_fence *fence,
bool intr, signed long timeout)
{
/*
* If the waiter requests to wait with no timeout, force a timeout to ensure
* that it won't get stuck forever in the kernel if something were to go
* wrong with signaling, such as a malicious userspace not releasing the
* semaphore.
*
* 96 ms (roughly 6 frames @ 60 Hz) is arbitrarily chosen to be long enough
* that it should never get hit during normal operation, but not so long
* that the system becomes unresponsive.
*/
return dma_fence_default_wait(fence, intr,
(timeout == MAX_SCHEDULE_TIMEOUT) ?
msecs_to_jiffies(96) : timeout);
}
static const struct dma_fence_ops nv_drm_gem_prime_fence_ops = {
.get_driver_name = nv_drm_gem_fence_op_get_driver_name,
.get_timeline_name = nv_drm_gem_prime_fence_op_get_timeline_name,
.enable_signaling = nv_drm_gem_prime_fence_op_enable_signaling,
.release = nv_drm_gem_prime_fence_op_release,
.wait = nv_drm_gem_prime_fence_op_wait,
};
static inline void
__nv_drm_prime_fence_signal(struct nv_drm_prime_fence *nv_fence)
{
list_del(&nv_fence->list_entry);
dma_fence_signal(&nv_fence->base);
dma_fence_put(&nv_fence->base);
}
static void nv_drm_gem_prime_force_fence_signal(
struct nv_drm_prime_fence_context *nv_fence_context)
{
WARN_ON(!spin_is_locked(&nv_fence_context->lock));
while (!list_empty(&nv_fence_context->pending)) {
struct nv_drm_prime_fence *nv_fence = list_first_entry(
&nv_fence_context->pending,
typeof(*nv_fence),
list_entry);
__nv_drm_prime_fence_signal(nv_fence);
}
}
static void nv_drm_gem_prime_fence_event
(
void *dataPtr,
NvU32 dataU32
)
{
struct nv_drm_prime_fence_context *nv_fence_context = dataPtr;
spin_lock(&nv_fence_context->lock);
while (!list_empty(&nv_fence_context->pending)) {
struct nv_drm_prime_fence *nv_fence = list_first_entry(
&nv_fence_context->pending,
typeof(*nv_fence),
list_entry);
/* Index into surface with 16 byte stride */
unsigned int seqno = *((nv_fence_context->pLinearAddress) +
(nv_fence_context->base.fenceSemIndex * 4));
if (nv_fence->base.seqno > seqno) {
/*
* Fences in list are placed in increasing order of sequence
* number, breaks a loop once found first fence not
* ready to signal.
*/
break;
}
__nv_drm_prime_fence_signal(nv_fence);
}
spin_unlock(&nv_fence_context->lock);
}
static inline struct nv_drm_prime_fence_context*
to_nv_prime_fence_context(struct nv_drm_fence_context *nv_fence_context) {
return container_of(nv_fence_context, struct nv_drm_prime_fence_context, base);
}
static void __nv_drm_prime_fence_context_destroy(
struct nv_drm_fence_context *nv_fence_context)
{
struct nv_drm_device *nv_dev = nv_fence_context->nv_dev;
struct nv_drm_prime_fence_context *nv_prime_fence_context =
to_nv_prime_fence_context(nv_fence_context);
/*
* Skip nvKms calls if device is being surprise-removed.
* The nvidia_modeset internal state may be corrupted.
*/
if (nv_dev->pDevice == NULL || nv_dev->inSurpriseRemoval) {
/* Force signal pending fences and free */
spin_lock(&nv_prime_fence_context->lock);
nv_drm_gem_prime_force_fence_signal(nv_prime_fence_context);
spin_unlock(&nv_prime_fence_context->lock);
nv_drm_free(nv_fence_context);
return;
}
/*
* Free channel event before destroying the fence context, otherwise event
* callback continue to get called.
*/
nvKms->freeChannelEvent(nv_dev->pDevice, nv_prime_fence_context->cb);
/* Force signal all pending fences and empty pending list */
spin_lock(&nv_prime_fence_context->lock);
nv_drm_gem_prime_force_fence_signal(nv_prime_fence_context);
spin_unlock(&nv_prime_fence_context->lock);
/* Free nvkms resources */
nvKms->unmapMemory(nv_dev->pDevice,
nv_prime_fence_context->pSemSurface,
NVKMS_KAPI_MAPPING_TYPE_KERNEL,
(void *) nv_prime_fence_context->pLinearAddress);
nvKms->freeMemory(nv_dev->pDevice, nv_prime_fence_context->pSemSurface);
nv_drm_free(nv_fence_context);
}
static struct nv_drm_fence_context_ops nv_drm_prime_fence_context_ops = {
.destroy = __nv_drm_prime_fence_context_destroy,
};
static inline struct nv_drm_prime_fence_context *
__nv_drm_prime_fence_context_new(
struct nv_drm_device *nv_dev,
struct drm_nvidia_prime_fence_context_create_params *p)
{
struct nv_drm_prime_fence_context *nv_prime_fence_context;
struct NvKmsKapiMemory *pSemSurface;
NvU32 *pLinearAddress;
/* Allocate backup nvkms resources */
pSemSurface = nvKms->importMemory(nv_dev->pDevice,
p->size,
p->import_mem_nvkms_params_ptr,
p->import_mem_nvkms_params_size);
if (!pSemSurface) {
NV_DRM_DEV_LOG_ERR(
nv_dev,
"Failed to import fence semaphore surface");
goto failed;
}
if (!nvKms->mapMemory(nv_dev->pDevice,
pSemSurface,
NVKMS_KAPI_MAPPING_TYPE_KERNEL,
(void **) &pLinearAddress)) {
NV_DRM_DEV_LOG_ERR(
nv_dev,
"Failed to map fence semaphore surface");
goto failed_to_map_memory;
}
/*
* Allocate a fence context object, initialize it and allocate channel
* event for it.
*/
if ((nv_prime_fence_context = nv_drm_calloc(
1,
sizeof(*nv_prime_fence_context))) == NULL) {
goto failed_alloc_fence_context;
}
/*
* dma_fence_context_alloc() cannot fail, so we do not need
* to check a return value.
*/
nv_prime_fence_context->base.ops = &nv_drm_prime_fence_context_ops;
nv_prime_fence_context->base.nv_dev = nv_dev;
nv_prime_fence_context->base.context = dma_fence_context_alloc(1);
nv_prime_fence_context->base.fenceSemIndex = p->index;
nv_prime_fence_context->pSemSurface = pSemSurface;
nv_prime_fence_context->pLinearAddress = pLinearAddress;
INIT_LIST_HEAD(&nv_prime_fence_context->pending);
spin_lock_init(&nv_prime_fence_context->lock);
/*
* Except 'cb', the fence context should be completely initialized
* before channel event allocation because the fence context may start
* receiving events immediately after allocation.
*
* There are no simultaneous read/write access to 'cb', therefore it does
* not require spin-lock protection.
*/
nv_prime_fence_context->cb =
nvKms->allocateChannelEvent(nv_dev->pDevice,
nv_drm_gem_prime_fence_event,
nv_prime_fence_context,
p->event_nvkms_params_ptr,
p->event_nvkms_params_size);
if (!nv_prime_fence_context->cb) {
NV_DRM_DEV_LOG_ERR(nv_dev,
"Failed to allocate fence signaling event");
goto failed_to_allocate_channel_event;
}
return nv_prime_fence_context;
failed_to_allocate_channel_event:
nv_drm_free(nv_prime_fence_context);
failed_alloc_fence_context:
nvKms->unmapMemory(nv_dev->pDevice,
pSemSurface,
NVKMS_KAPI_MAPPING_TYPE_KERNEL,
(void *) pLinearAddress);
failed_to_map_memory:
nvKms->freeMemory(nv_dev->pDevice, pSemSurface);
failed:
return NULL;
}
static struct dma_fence *__nv_drm_prime_fence_context_create_fence(
struct nv_drm_prime_fence_context *nv_prime_fence_context,
unsigned int seqno)
{
struct nv_drm_prime_fence *nv_fence;
int ret = 0;
if ((nv_fence = nv_drm_calloc(1, sizeof(*nv_fence))) == NULL) {
ret = -ENOMEM;
goto out;
}
spin_lock(&nv_prime_fence_context->lock);
/*
* If seqno wrapped, force signal fences to make sure none of them
* get stuck.
*/
if (seqno < nv_prime_fence_context->last_seqno) {
nv_drm_gem_prime_force_fence_signal(nv_prime_fence_context);
}
INIT_LIST_HEAD(&nv_fence->list_entry);
spin_lock_init(&nv_fence->lock);
dma_fence_init(&nv_fence->base, &nv_drm_gem_prime_fence_ops,
&nv_fence->lock, nv_prime_fence_context->base.context,
seqno);
/* The context maintains a reference to any pending fences. */
dma_fence_get(&nv_fence->base);
list_add_tail(&nv_fence->list_entry, &nv_prime_fence_context->pending);
nv_prime_fence_context->last_seqno = seqno;
spin_unlock(&nv_prime_fence_context->lock);
out:
return ret != 0 ? ERR_PTR(ret) : &nv_fence->base;
}
int nv_drm_fence_supported_ioctl(struct drm_device *dev,
void *data, struct drm_file *filep)
{
struct nv_drm_device *nv_dev = to_nv_device(dev);
return nv_dev->pDevice ? 0 : -EINVAL;
}
static inline struct nv_drm_fence_context *to_nv_fence_context(
struct nv_drm_gem_object *nv_gem)
{
if (nv_gem != NULL) {
return container_of(nv_gem, struct nv_drm_fence_context, base);
}
return NULL;
}
/*
* Tear down of the 'struct nv_drm_fence_context' object is not expected
* to be happen from any worker thread, if that happen it causes dead-lock
* because tear down sequence calls to flush all existing
* worker thread.
*/
static void
__nv_drm_fence_context_gem_free(struct nv_drm_gem_object *nv_gem)
{
struct nv_drm_fence_context *nv_fence_context = to_nv_fence_context(nv_gem);
nv_fence_context->ops->destroy(nv_fence_context);
}
const struct nv_drm_gem_object_funcs nv_fence_context_gem_ops = {
.free = __nv_drm_fence_context_gem_free,
};
static inline
struct nv_drm_fence_context *
__nv_drm_fence_context_lookup(
struct drm_file *filp,
u32 handle)
{
struct nv_drm_gem_object *nv_gem =
nv_drm_gem_object_lookup(filp, handle);
if (nv_gem != NULL && nv_gem->ops != &nv_fence_context_gem_ops) {
nv_drm_gem_object_unreference_unlocked(nv_gem);
return NULL;
}
return to_nv_fence_context(nv_gem);
}
static int
__nv_drm_fence_context_gem_init(struct drm_device *dev,
struct nv_drm_fence_context *nv_fence_context,
u32 *handle,
struct drm_file *filep)
{
struct nv_drm_device *nv_dev = to_nv_device(dev);
nv_drm_gem_object_init(nv_dev,
&nv_fence_context->base,
&nv_fence_context_gem_ops,
0 /* size */,
NULL /* pMemory */);
return nv_drm_gem_handle_create_drop_reference(filep,
&nv_fence_context->base,
handle);
}
int nv_drm_prime_fence_context_create_ioctl(struct drm_device *dev,
void *data, struct drm_file *filep)
{
struct nv_drm_device *nv_dev = to_nv_device(dev);
struct drm_nvidia_prime_fence_context_create_params *p = data;
struct nv_drm_prime_fence_context *nv_prime_fence_context;
int err;
if (nv_dev->pDevice == NULL) {
return -EOPNOTSUPP;
}
nv_prime_fence_context = __nv_drm_prime_fence_context_new(nv_dev, p);
if (!nv_prime_fence_context) {
goto done;
}
err = __nv_drm_fence_context_gem_init(dev,
&nv_prime_fence_context->base,
&p->handle,
filep);
if (err) {
__nv_drm_prime_fence_context_destroy(&nv_prime_fence_context->base);
}
return err;
done:
return -ENOMEM;
}
static int __nv_drm_gem_attach_fence(struct nv_drm_gem_object *nv_gem,
struct dma_fence *fence,
bool shared)
{
nv_dma_resv_t *resv = nv_drm_gem_res_obj(nv_gem);
int ret;
nv_dma_resv_lock(resv, NULL);
ret = nv_dma_resv_reserve_fences(resv, 1, shared);
if (ret == 0) {
if (shared) {
nv_dma_resv_add_shared_fence(resv, fence);
} else {
nv_dma_resv_add_excl_fence(resv, fence);
}
} else {
NV_DRM_LOG_ERR("Failed to reserve fence. Error code: %d", ret);
}
nv_dma_resv_unlock(resv);
return ret;
}
int nv_drm_gem_prime_fence_attach_ioctl(struct drm_device *dev,
void *data, struct drm_file *filep)
{
int ret = -EINVAL;
struct nv_drm_device *nv_dev = to_nv_device(dev);
struct drm_nvidia_gem_prime_fence_attach_params *p = data;
struct nv_drm_gem_object *nv_gem;
struct nv_drm_fence_context *nv_fence_context;
struct dma_fence *fence;
if (nv_dev->pDevice == NULL) {
ret = -EOPNOTSUPP;
goto done;
}
if (p->__pad != 0) {
NV_DRM_DEV_LOG_ERR(nv_dev, "Padding fields must be zeroed");
goto done;
}
nv_gem = nv_drm_gem_object_lookup(filep, p->handle);
if (!nv_gem) {
NV_DRM_DEV_LOG_ERR(
nv_dev,
"Failed to lookup gem object for fence attach: 0x%08x",
p->handle);
goto done;
}
if((nv_fence_context = __nv_drm_fence_context_lookup(
filep,
p->fence_context_handle)) == NULL) {
NV_DRM_DEV_LOG_ERR(
nv_dev,
"Failed to lookup gem object for fence context: 0x%08x",
p->fence_context_handle);
goto fence_context_lookup_failed;
}
if (nv_fence_context->ops !=
&nv_drm_prime_fence_context_ops) {
NV_DRM_DEV_LOG_ERR(
nv_dev,
"Wrong fence context type: 0x%08x",
p->fence_context_handle);
goto fence_context_create_fence_failed;
}
fence = __nv_drm_prime_fence_context_create_fence(
to_nv_prime_fence_context(nv_fence_context),
p->sem_thresh);
if (IS_ERR(fence)) {
ret = PTR_ERR(fence);
NV_DRM_DEV_LOG_ERR(
nv_dev,
"Failed to allocate fence: 0x%08x", p->handle);
goto fence_context_create_fence_failed;
}
ret = __nv_drm_gem_attach_fence(nv_gem, fence, true /* exclusive */);
dma_fence_put(fence);
fence_context_create_fence_failed:
nv_drm_gem_object_unreference_unlocked(&nv_fence_context->base);
fence_context_lookup_failed:
nv_drm_gem_object_unreference_unlocked(nv_gem);
done:
return ret;
}
struct nv_drm_semsurf_fence {
struct dma_fence base;
spinlock_t lock;
/*
* When unsignaled, node in the associated fence context's pending fence
* list. The list holds a reference to the fence
*/
struct list_head pending_node;
#if !defined(NV_DMA_FENCE_OPS_HAS_USE_64BIT_SEQNO)
/* 64-bit version of base.seqno on kernels with 32-bit fence seqno */
NvU64 wait_value;
#endif
/*
* Raw absolute kernel time (time domain and scale are treated as opaque)
* when this fence times out.
*/
unsigned long timeout;
};
struct nv_drm_semsurf_fence_callback {
struct nv_drm_semsurf_fence_ctx *ctx;
nv_drm_work work;
NvU64 wait_value;
};
struct nv_drm_sync_fd_wait_data {
struct dma_fence_cb dma_fence_cb;
struct nv_drm_semsurf_fence_ctx *ctx;
nv_drm_work work; /* Deferred second half of fence wait callback */
/* Could use a lockless list data structure here instead */
struct list_head pending_node;
NvU64 pre_wait_value;
NvU64 post_wait_value;
};
struct nv_drm_semsurf_fence_ctx {
struct nv_drm_fence_context base;
/* The NVKMS KAPI reference to the context's semaphore surface */
struct NvKmsKapiSemaphoreSurface *pSemSurface;
/* CPU mapping of the semaphore slot values */
union {
volatile void *pVoid;
volatile NvU32 *p32;
volatile NvU64 *p64;
} pSemMapping;
volatile NvU64 *pMaxSubmittedMapping;
/* work thread for fence timeouts and waits */
nv_drm_workthread worker;
/* Timeout timer and associated workthread work */
nv_drm_timer timer;
nv_drm_work timeout_work;
/* Protects access to everything below */
spinlock_t lock;
/* List of pending fences which are not yet signaled */
struct list_head pending_fences;
/* List of pending fence wait operations */
struct list_head pending_waits;
/*
* Tracking data for the single in-flight callback associated with this
* context. Either both pointers will be valid, or both will be NULL.
*
* Note it is not safe to dereference these values outside of the context
* lock unless it is certain the associated callback is not yet active,
* or has been canceled. Their memory is owned by the callback itself as
* soon as it is registered. Subtly, this means these variables can not
* be used as output parameters to the function that registers the callback.
*/
struct {
struct nv_drm_semsurf_fence_callback *local;
struct NvKmsKapiSemaphoreSurfaceCallback *nvKms;
} callback;
/*
* Wait value associated with either the above or a being-registered
* callback. May differ from callback->local->wait_value if it is the
* latter. Zero if no callback is currently needed.
*/
NvU64 current_wait_value;
};
static inline struct nv_drm_semsurf_fence_ctx*
to_semsurf_fence_ctx(
struct nv_drm_fence_context *nv_fence_context
)
{
return container_of(nv_fence_context,
struct nv_drm_semsurf_fence_ctx,
base);
}
static inline NvU64
__nv_drm_get_semsurf_fence_seqno(const struct nv_drm_semsurf_fence *nv_fence)
{
#if defined(NV_DMA_FENCE_OPS_HAS_USE_64BIT_SEQNO)
return nv_fence->base.seqno;
#else
return nv_fence->wait_value;
#endif
}
#ifndef READ_ONCE
#define READ_ONCE(x) ACCESS_ONCE(x)
#endif
static inline NvU64
__nv_drm_get_semsurf_ctx_seqno(struct nv_drm_semsurf_fence_ctx *ctx)
{
NvU64 semVal;
if (ctx->pMaxSubmittedMapping) {
/* 32-bit GPU semaphores */
NvU64 maxSubmitted = READ_ONCE(*ctx->pMaxSubmittedMapping);
/*
* Must happen after the max submitted read! See
* NvTimeSemFermiGetPayload() for full details.
*/
semVal = READ_ONCE(*ctx->pSemMapping.p32);
if ((maxSubmitted & 0xFFFFFFFFull) < semVal) {
maxSubmitted -= 0x100000000ull;
}
semVal |= (maxSubmitted & 0xffffffff00000000ull);
} else {
/* 64-bit GPU semaphores */
semVal = READ_ONCE(*ctx->pSemMapping.p64);
}
return semVal;
}
static void
__nv_drm_semsurf_force_complete_pending(struct nv_drm_semsurf_fence_ctx *ctx)
{
unsigned long flags;
/*
* No locks are needed for the pending_fences list. This code runs after all
* other possible references to the fence context have been removed. The
* fences have their own individual locks to protect themselves.
*/
while (!list_empty(&ctx->pending_fences)) {
struct nv_drm_semsurf_fence *nv_fence = list_first_entry(
&ctx->pending_fences,
typeof(*nv_fence),
pending_node);
struct dma_fence *fence = &nv_fence->base;
list_del(&nv_fence->pending_node);
dma_fence_set_error(fence, -ETIMEDOUT);
dma_fence_signal(fence);
/* Remove the pending list's reference */
dma_fence_put(fence);
}
/*
* The pending waits are also referenced by the fences they are waiting on,
* but those fences are guaranteed to complete in finite time. Just keep the
* the context alive until they do so.
*/
spin_lock_irqsave(&ctx->lock, flags);
while (!list_empty(&ctx->pending_waits)) {
spin_unlock_irqrestore(&ctx->lock, flags);
nv_drm_yield();
spin_lock_irqsave(&ctx->lock, flags);
}
spin_unlock_irqrestore(&ctx->lock, flags);
}
/* Forward declaration */
static void
__nv_drm_semsurf_ctx_reg_callbacks(struct nv_drm_semsurf_fence_ctx *ctx);
static void
__nv_drm_semsurf_ctx_fence_callback_work(void *data)
{
struct nv_drm_semsurf_fence_callback *callback = data;
__nv_drm_semsurf_ctx_reg_callbacks(callback->ctx);
nv_drm_free(callback);
}
static struct nv_drm_semsurf_fence_callback*
__nv_drm_semsurf_new_callback(struct nv_drm_semsurf_fence_ctx *ctx)
{
struct nv_drm_semsurf_fence_callback *newCallback =
nv_drm_calloc(1, sizeof(*newCallback));
if (!newCallback) {
return NULL;
}
newCallback->ctx = ctx;
nv_drm_workthread_work_init(&newCallback->work,
__nv_drm_semsurf_ctx_fence_callback_work,
newCallback);
return newCallback;
}
static void
__nv_drm_semsurf_ctx_process_completed(struct nv_drm_semsurf_fence_ctx *ctx,
NvU64 *newWaitValueOut,
unsigned long *newTimeoutOut)
{
struct list_head finished;
struct list_head timed_out;
struct nv_drm_semsurf_fence *nv_fence;
struct dma_fence *fence;
NvU64 currentSeqno = __nv_drm_get_semsurf_ctx_seqno(ctx);
NvU64 fenceSeqno = 0;
unsigned long flags;
unsigned long fenceTimeout = 0;
unsigned long now = nv_drm_timer_now();
INIT_LIST_HEAD(&finished);
INIT_LIST_HEAD(&timed_out);
spin_lock_irqsave(&ctx->lock, flags);
while (!list_empty(&ctx->pending_fences)) {
nv_fence = list_first_entry(&ctx->pending_fences,
typeof(*nv_fence),
pending_node);
fenceSeqno = __nv_drm_get_semsurf_fence_seqno(nv_fence);
fenceTimeout = nv_fence->timeout;
if (fenceSeqno <= currentSeqno) {
list_move_tail(&nv_fence->pending_node, &finished);
} else if (fenceTimeout <= now) {
list_move_tail(&nv_fence->pending_node, &timed_out);
} else {
break;
}
}
/*
* If the caller passes non-NULL newWaitValueOut and newTimeoutOut
* parameters, it establishes a contract. If the returned values are
* non-zero, the caller must attempt to register a callback associated with
* the new wait value and reset the context's timer to the specified
* timeout.
*/
if (newWaitValueOut && newTimeoutOut) {
if (list_empty(&ctx->pending_fences)) {
/* No pending fences, so no waiter is needed. */
ctx->current_wait_value = fenceSeqno = 0;
fenceTimeout = 0;
} else if (fenceSeqno == ctx->current_wait_value) {
/*
* The context already has a waiter registered, or in the process of
* being registered, for this fence. Indicate to the caller no new
* waiter registration is needed, and leave the ctx state alone.
*/
fenceSeqno = 0;
fenceTimeout = 0;
} else {
/* A new waiter must be registered. Prep the context */
ctx->current_wait_value = fenceSeqno;
}
*newWaitValueOut = fenceSeqno;
*newTimeoutOut = fenceTimeout;
}
spin_unlock_irqrestore(&ctx->lock, flags);
while (!list_empty(&finished)) {
nv_fence = list_first_entry(&finished, typeof(*nv_fence), pending_node);
list_del_init(&nv_fence->pending_node);
fence = &nv_fence->base;
dma_fence_signal(fence);
dma_fence_put(fence); /* Drops the pending list's reference */
}
while (!list_empty(&timed_out)) {
nv_fence = list_first_entry(&timed_out, typeof(*nv_fence),
pending_node);
list_del_init(&nv_fence->pending_node);
fence = &nv_fence->base;
dma_fence_set_error(fence, -ETIMEDOUT);
dma_fence_signal(fence);
dma_fence_put(fence); /* Drops the pending list's reference */
}
}
static void
__nv_drm_semsurf_ctx_callback(void *data)
{
struct nv_drm_semsurf_fence_callback *callback = data;
struct nv_drm_semsurf_fence_ctx *ctx = callback->ctx;
unsigned long flags;
spin_lock_irqsave(&ctx->lock, flags);
/* If this was the context's currently registered callback, clear it. */
if (ctx->callback.local == callback) {
ctx->callback.local = NULL;
ctx->callback.nvKms = NULL;
}
/* If storing of this callback may have been pending, prevent it. */
if (ctx->current_wait_value == callback->wait_value) {
ctx->current_wait_value = 0;
}
spin_unlock_irqrestore(&ctx->lock, flags);
/*
* This is redundant with the __nv_drm_semsurf_ctx_reg_callbacks() call from
* __nv_drm_semsurf_ctx_fence_callback_work(), which will be called by the
* work enqueued below, but calling it here as well allows unblocking
* waiters with less latency.
*/
__nv_drm_semsurf_ctx_process_completed(ctx, NULL, NULL);
if (!nv_drm_workthread_add_work(&ctx->worker, &callback->work)) {
/*
* The context is shutting down. It will force-signal all fences when
* doing so, so there's no need for any more callback handling.
*/
nv_drm_free(callback);
}
}
/*
* Take spin lock, attempt to stash newNvKmsCallback/newCallback in ctx.
* If current_wait_value in fence context != new_wait_value, we raced with
* someone registering a newer waiter. Release spin lock, and unregister our
* waiter. It isn't needed anymore.
*/
static bool
__nv_drm_semsurf_ctx_store_callback(
struct nv_drm_semsurf_fence_ctx *ctx,
NvU64 new_wait_value,
struct NvKmsKapiSemaphoreSurfaceCallback *newNvKmsCallback,
struct nv_drm_semsurf_fence_callback *newCallback)
{
struct nv_drm_device *nv_dev = ctx->base.nv_dev;
struct NvKmsKapiSemaphoreSurfaceCallback *oldNvKmsCallback;
struct nv_drm_semsurf_fence_callback *oldCallback = NULL;
NvU64 oldWaitValue;
unsigned long flags;
bool installed = false;
spin_lock_irqsave(&ctx->lock, flags);
if (ctx->current_wait_value == new_wait_value) {
oldCallback = ctx->callback.local;
oldNvKmsCallback = ctx->callback.nvKms;
oldWaitValue = oldCallback ? oldCallback->wait_value : 0;
ctx->callback.local = newCallback;
ctx->callback.nvKms = newNvKmsCallback;
installed = true;
}
spin_unlock_irqrestore(&ctx->lock, flags);
if (oldCallback) {
if (nvKms->unregisterSemaphoreSurfaceCallback(nv_dev->pDevice,
ctx->pSemSurface,
ctx->base.fenceSemIndex,
oldWaitValue,
oldNvKmsCallback)) {
/*
* The old callback was successfully canceled, and its NVKMS and RM
* resources have been freed. Free its local tracking data.
*/
nv_drm_free(oldCallback);
} else {
/*
* The new callback is already running. It will do no harm, and free
* itself.
*/
}
}