-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathnvidia-modeset-linux.c
More file actions
2260 lines (1810 loc) · 57.9 KB
/
nvidia-modeset-linux.c
File metadata and controls
2260 lines (1810 loc) · 57.9 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2015-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: MIT
*
* 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 <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/vmalloc.h>
#include <asm/div64.h> /* do_div() */
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/random.h>
#include <linux/file.h>
#include <linux/list.h>
#include <linux/rwsem.h>
#include <linux/freezer.h>
#include <linux/poll.h>
#include <linux/cdev.h>
#include <acpi/video.h>
#include "nvstatus.h"
#include "nv-modeset-interface.h"
#include "nv-kref.h"
#include "nvidia-modeset-os-interface.h"
#include "nvkms.h"
#include "nvkms-ioctl.h"
#include "conftest.h"
#include "nv-procfs.h"
#include "nv-kthread-q.h"
#include "nv-time.h"
#include "nv-timer.h"
#include "nv-lock.h"
#include "nv-chardev-numbers.h"
/*
* Commit aefb2f2e619b ("x86/bugs: Rename CONFIG_RETPOLINE =>
* CONFIG_MITIGATION_RETPOLINE) in v6.8 renamed CONFIG_RETPOLINE.
*/
#if !defined(CONFIG_RETPOLINE) && !defined(CONFIG_MITIGATION_RETPOLINE)
#include "nv-retpoline.h"
#endif
#include <linux/backlight.h>
#define NVKMS_LOG_PREFIX "nvidia-modeset: "
static bool output_rounding_fix = true;
module_param_named(output_rounding_fix, output_rounding_fix, bool, 0400);
static bool disable_hdmi_frl = false;
module_param_named(disable_hdmi_frl, disable_hdmi_frl, bool, 0400);
static bool disable_vrr_memclk_switch = false;
module_param_named(disable_vrr_memclk_switch, disable_vrr_memclk_switch, bool, 0400);
static bool hdmi_deepcolor = true;
module_param_named(hdmi_deepcolor, hdmi_deepcolor, bool, 0400);
static bool vblank_sem_control = true;
module_param_named(vblank_sem_control, vblank_sem_control, bool, 0400);
static bool opportunistic_display_sync = true;
module_param_named(opportunistic_display_sync, opportunistic_display_sync, bool, 0400);
static enum NvKmsDebugForceColorSpace debug_force_color_space = NVKMS_DEBUG_FORCE_COLOR_SPACE_NONE;
module_param_named(debug_force_color_space, debug_force_color_space, uint, 0400);
static bool enable_overlay_layers = true;
module_param_named(enable_overlay_layers, enable_overlay_layers, bool, 0400);
/* These parameters are used for fault injection tests. Normally the defaults
* should be used. */
MODULE_PARM_DESC(fail_malloc, "Fail the Nth call to nvkms_alloc");
static int fail_malloc_num = -1;
module_param_named(fail_malloc, fail_malloc_num, int, 0400);
MODULE_PARM_DESC(malloc_verbose, "Report information about malloc calls on module unload");
static bool malloc_verbose = false;
module_param_named(malloc_verbose, malloc_verbose, bool, 0400);
MODULE_PARM_DESC(conceal_vrr_caps,
"Conceal all display VRR capabilities");
static bool conceal_vrr_caps = false;
module_param_named(conceal_vrr_caps, conceal_vrr_caps, bool, 0400);
/* Fail allocating the RM core channel for NVKMS using the i-th method (see
* FailAllocCoreChannelMethod). Failures not using the i-th method are ignored. */
MODULE_PARM_DESC(fail_alloc_core_channel, "Control testing for hardware core channel allocation failure");
static int fail_alloc_core_channel_method = -1;
module_param_named(fail_alloc_core_channel, fail_alloc_core_channel_method, int, 0400);
MODULE_PARM_DESC(debug, "Enable debug logging");
static int debug = 0;
module_param_named(debug, debug, int, 0600);
#if NVKMS_CONFIG_FILE_SUPPORTED
/* This parameter is used to find the dpy override conf file */
#define NVKMS_CONF_FILE_SPECIFIED (nvkms_conf != NULL)
MODULE_PARM_DESC(config_file,
"Path to the nvidia-modeset configuration file (default: disabled)");
static char *nvkms_conf = NULL;
module_param_named(config_file, nvkms_conf, charp, 0400);
#endif
static atomic_t nvkms_alloc_called_count;
#define NV_SUPPORTS_PLATFORM_DEVICE_PUT NV_IS_EXPORT_SYMBOL_GPL_platform_device_put
#if defined(NV_LINUX_NVHOST_H_PRESENT) && NV_SUPPORTS_PLATFORM_DEVICE_PUT
#if defined(NV_LINUX_HOST1X_NEXT_H_PRESENT) || defined(CONFIG_TEGRA_GRHOST)
#define NVKMS_NVHOST_SYNCPT_SUPPORTED
struct platform_device *nvhost_platform_device = NULL;
#endif
#endif
NvBool nvkms_test_fail_alloc_core_channel(
enum FailAllocCoreChannelMethod method
)
{
if (method != fail_alloc_core_channel_method) {
// don't fail if it's not the currently specified method
return NV_FALSE;
}
printk(KERN_INFO NVKMS_LOG_PREFIX
"Failing core channel allocation using method %d",
fail_alloc_core_channel_method);
return NV_TRUE;
}
NvBool nvkms_conceal_vrr_caps(void)
{
return conceal_vrr_caps;
}
NvBool nvkms_output_rounding_fix(void)
{
return output_rounding_fix;
}
NvBool nvkms_disable_hdmi_frl(void)
{
return disable_hdmi_frl;
}
NvBool nvkms_disable_vrr_memclk_switch(void)
{
return disable_vrr_memclk_switch;
}
NvBool nvkms_hdmi_deepcolor(void)
{
return hdmi_deepcolor;
}
NvBool nvkms_vblank_sem_control(void)
{
return vblank_sem_control;
}
NvBool nvkms_opportunistic_display_sync(void)
{
return opportunistic_display_sync;
}
enum NvKmsDebugForceColorSpace nvkms_debug_force_color_space(void)
{
if (debug_force_color_space >= NVKMS_DEBUG_FORCE_COLOR_SPACE_MAX) {
return NVKMS_DEBUG_FORCE_COLOR_SPACE_NONE;
}
return debug_force_color_space;
}
NvBool nvkms_enable_overlay_layers(void)
{
return enable_overlay_layers;
}
NvBool nvkms_debug_logging(void)
{
return debug != 0;
}
NvBool nvkms_kernel_supports_syncpts(void)
{
/*
* Note this only checks that the kernel has the prerequisite
* support for syncpts; callers must also check that the hardware
* supports syncpts.
*/
#if defined(NVKMS_NVHOST_SYNCPT_SUPPORTED)
return NV_TRUE;
#else
return NV_FALSE;
#endif
}
/*************************************************************************
* NVKMS interface for nvhost unit for sync point APIs.
*************************************************************************/
#if defined(NVKMS_NVHOST_SYNCPT_SUPPORTED) && defined(CONFIG_TEGRA_GRHOST)
#include <linux/nvhost.h>
NvBool nvkms_syncpt_op(
enum NvKmsSyncPtOp op,
NvKmsSyncPtOpParams *params)
{
if (nvhost_platform_device == NULL) {
nvkms_log(NVKMS_LOG_LEVEL_ERROR, NVKMS_LOG_PREFIX,
"Failed to get default nvhost device");
return NV_FALSE;
}
switch (op) {
case NVKMS_SYNCPT_OP_ALLOC:
params->alloc.id = nvhost_get_syncpt_client_managed(
nvhost_platform_device, params->alloc.syncpt_name);
break;
case NVKMS_SYNCPT_OP_PUT:
nvhost_syncpt_put_ref_ext(nvhost_platform_device, params->put.id);
break;
case NVKMS_SYNCPT_OP_FD_TO_ID_AND_THRESH: {
struct nvhost_fence *fence;
NvU32 id, thresh;
fence = nvhost_fence_get(params->fd_to_id_and_thresh.fd);
if (fence == NULL) {
return NV_FALSE;
}
if (nvhost_fence_num_pts(fence) > 1) {
/*! Syncpoint fence fd contains more than one syncpoint */
nvhost_fence_put(fence);
return NV_FALSE;
}
if (nvhost_fence_get_pt(fence, 0, &id, &thresh) != 0) {
nvhost_fence_put(fence);
return NV_FALSE;
}
params->fd_to_id_and_thresh.id = id;
params->fd_to_id_and_thresh.thresh = thresh;
nvhost_fence_put(fence);
break;
}
case NVKMS_SYNCPT_OP_ID_AND_THRESH_TO_FD:
nvhost_syncpt_create_fence_single_ext(
nvhost_platform_device,
params->id_and_thresh_to_fd.id,
params->id_and_thresh_to_fd.thresh,
"nvkms-fence",
¶ms->id_and_thresh_to_fd.fd);
break;
case NVKMS_SYNCPT_OP_READ_MINVAL:
params->read_minval.minval =
nvhost_syncpt_read_minval(nvhost_platform_device, params->read_minval.id);
break;
}
return NV_TRUE;
}
#elif defined(NVKMS_NVHOST_SYNCPT_SUPPORTED) && defined(NV_LINUX_HOST1X_NEXT_H_PRESENT)
#include <linux/dma-fence.h>
#include <linux/file.h>
#include <linux/host1x-next.h>
#include <linux/sync_file.h>
/*
* If the host1x.h header is present, then we are using the upstream
* host1x driver and so make sure CONFIG_TEGRA_HOST1X is defined to pick
* up the correct prototypes/definitions in nvhost.h.
*/
#define CONFIG_TEGRA_HOST1X
#include <linux/nvhost.h>
NvBool nvkms_syncpt_op(
enum NvKmsSyncPtOp op,
NvKmsSyncPtOpParams *params)
{
struct host1x_syncpt *host1x_sp;
struct host1x *host1x;
if (nvhost_platform_device == NULL) {
nvkms_log(NVKMS_LOG_LEVEL_ERROR, NVKMS_LOG_PREFIX,
"Failed to get default nvhost device");
return NV_FALSE;
}
host1x = nvhost_get_host1x(nvhost_platform_device);
if (host1x == NULL) {
nvkms_log(NVKMS_LOG_LEVEL_ERROR, NVKMS_LOG_PREFIX,
"Failed to get host1x");
return NV_FALSE;
}
switch (op) {
case NVKMS_SYNCPT_OP_ALLOC:
host1x_sp = host1x_syncpt_alloc(host1x,
HOST1X_SYNCPT_CLIENT_MANAGED,
params->alloc.syncpt_name);
if (host1x_sp == NULL) {
return NV_FALSE;
}
params->alloc.id = host1x_syncpt_id(host1x_sp);
break;
case NVKMS_SYNCPT_OP_PUT:
host1x_sp = host1x_syncpt_get_by_id_noref(host1x, params->put.id);
if (host1x_sp == NULL) {
return NV_FALSE;
}
host1x_syncpt_put(host1x_sp);
break;
case NVKMS_SYNCPT_OP_FD_TO_ID_AND_THRESH: {
struct dma_fence *f;
NvU32 id, thresh;
int err;
f = sync_file_get_fence(params->fd_to_id_and_thresh.fd);
if (f == NULL) {
return NV_FALSE;
}
if (dma_fence_is_array(f)) {
struct dma_fence_array *array = to_dma_fence_array(f);
if (array->num_fences > 1) {
/* Syncpoint fence fd contains more than one syncpoint */
dma_fence_put(f);
return NV_FALSE;
}
f = array->fences[0];
}
err = host1x_fence_extract(f, &id, &thresh);
dma_fence_put(f);
if (err < 0) {
return NV_FALSE;
}
params->fd_to_id_and_thresh.id = id;
params->fd_to_id_and_thresh.thresh = thresh;
break;
}
case NVKMS_SYNCPT_OP_ID_AND_THRESH_TO_FD: {
struct sync_file *file;
struct dma_fence *f;
int fd;
host1x_sp = host1x_syncpt_get_by_id_noref(host1x,
params->id_and_thresh_to_fd.id);
if (host1x_sp == NULL) {
return NV_FALSE;
}
f = host1x_fence_create(host1x_sp,
params->id_and_thresh_to_fd.thresh, true);
if (IS_ERR(f)) {
return NV_FALSE;
}
fd = get_unused_fd_flags(O_CLOEXEC);
if (fd < 0) {
dma_fence_put(f);
return NV_FALSE;
}
file = sync_file_create(f);
dma_fence_put(f);
if (!file) {
return NV_FALSE;
}
fd_install(fd, file->file);
params->id_and_thresh_to_fd.fd = fd;
break;
}
case NVKMS_SYNCPT_OP_READ_MINVAL:
host1x_sp = host1x_syncpt_get_by_id_noref(host1x, params->read_minval.id);
if (host1x_sp == NULL) {
return NV_FALSE;
}
params->read_minval.minval = host1x_syncpt_read(host1x_sp);
break;
}
return NV_TRUE;
}
#else
/* Unsupported STUB for nvkms_syncpt APIs */
NvBool nvkms_syncpt_op(
enum NvKmsSyncPtOp op,
NvKmsSyncPtOpParams *params)
{
return NV_FALSE;
}
#endif
#define NVKMS_MAJOR_DEVICE_NUMBER 195
#define NVKMS_MINOR_DEVICE_NUMBER 254
/*
* Convert from microseconds to jiffies. The conversion is:
* ((usec) * HZ / 1000000)
*
* Use do_div() to avoid gcc-generated references to __udivdi3().
* Note that the do_div() macro divides the first argument in place.
*/
static inline unsigned long NVKMS_USECS_TO_JIFFIES(NvU64 usec)
{
unsigned long result = usec * HZ;
do_div(result, 1000000);
return result;
}
/*************************************************************************
* NVKMS uses a global lock, nvkms_lock. The lock is taken in the
* file operation callback functions when calling into core NVKMS.
*************************************************************************/
static struct semaphore nvkms_lock;
/*************************************************************************
* User clients of NVKMS may need to be synchronized with suspend/resume
* operations. This depends on the state of the system when the NVKMS
* suspend/resume callbacks are invoked. NVKMS uses a single
* RW lock, nvkms_pm_lock, for this synchronization.
*************************************************************************/
static struct rw_semaphore nvkms_pm_lock;
/*************************************************************************
* NVKMS executes almost all of its queued work items on a single
* kthread. The exception are deferred close() handlers, which typically
* block for long periods of time and stall their queue.
*************************************************************************/
static struct nv_kthread_q nvkms_kthread_q;
static struct nv_kthread_q nvkms_deferred_close_kthread_q;
/*************************************************************************
* The nvkms_per_open structure tracks data that is specific to a
* single open.
*************************************************************************/
struct nvkms_per_open {
void *data;
enum NvKmsClientType type;
union {
struct {
struct {
atomic_t available;
wait_queue_head_t wait_queue;
} events;
} user;
struct {
struct {
nv_kthread_q_item_t nv_kthread_q_item;
} events;
} kernel;
} u;
nv_kthread_q_item_t deferred_close_q_item;
};
/*************************************************************************
* nvkms_pm_lock helper functions. Since no down_read_interruptible()
* or equivalent interface is available, it needs to be approximated with
* down_read_trylock() to enable the kernel's freezer to round up user
* threads going into suspend.
*************************************************************************/
static inline int nvkms_read_trylock_pm_lock(void)
{
return !down_read_trylock(&nvkms_pm_lock);
}
static inline void nvkms_read_lock_pm_lock(void)
{
if ((current->flags & PF_NOFREEZE)) {
/*
* Non-freezable tasks (i.e. kthreads in this case) don't have to worry
* about being frozen during system suspend, but do need to block so
* that the CPU can go idle during s2idle. Do a normal uninterruptible
* blocking wait for the PM lock.
*/
down_read(&nvkms_pm_lock);
} else {
/*
* For freezable tasks, make sure we give the kernel an opportunity to
* freeze if taking the PM lock fails.
*/
while (!down_read_trylock(&nvkms_pm_lock)) {
try_to_freeze();
cond_resched();
}
}
}
static inline void nvkms_read_unlock_pm_lock(void)
{
up_read(&nvkms_pm_lock);
}
static inline void nvkms_write_lock_pm_lock(void)
{
down_write(&nvkms_pm_lock);
}
static inline void nvkms_write_unlock_pm_lock(void)
{
up_write(&nvkms_pm_lock);
}
/*************************************************************************
* nvidia-modeset-os-interface.h functions. It is assumed that these
* are called while nvkms_lock is held.
*************************************************************************/
/* Don't use kmalloc for allocations larger than one page */
#define KMALLOC_LIMIT PAGE_SIZE
void* nvkms_alloc(size_t size, NvBool zero)
{
void *p;
if (malloc_verbose || fail_malloc_num >= 0) {
int this_alloc = atomic_inc_return(&nvkms_alloc_called_count) - 1;
if (fail_malloc_num >= 0 && fail_malloc_num == this_alloc) {
printk(KERN_WARNING NVKMS_LOG_PREFIX "Failing alloc %d\n",
fail_malloc_num);
return NULL;
}
}
if (size <= KMALLOC_LIMIT) {
p = kmalloc(size, GFP_KERNEL);
} else {
p = vmalloc(size);
}
if (zero && (p != NULL)) {
memset(p, 0, size);
}
return p;
}
void nvkms_free(void *ptr, size_t size)
{
if (size <= KMALLOC_LIMIT) {
kfree(ptr);
} else {
vfree(ptr);
}
}
void* nvkms_memset(void *ptr, NvU8 c, size_t size)
{
return memset(ptr, c, size);
}
void* nvkms_memcpy(void *dest, const void *src, size_t n)
{
return memcpy(dest, src, n);
}
void* nvkms_memmove(void *dest, const void *src, size_t n)
{
return memmove(dest, src, n);
}
int nvkms_memcmp(const void *s1, const void *s2, size_t n)
{
return memcmp(s1, s2, n);
}
size_t nvkms_strlen(const char *s)
{
return strlen(s);
}
int nvkms_strcmp(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}
char* nvkms_strncpy(char *dest, const char *src, size_t n)
{
return strncpy(dest, src, n);
}
void nvkms_usleep(NvU64 usec)
{
if (usec < 1000) {
/*
* If the period to wait is less than one millisecond, sleep
* using udelay(); note this is a busy wait.
*/
udelay(usec);
} else {
/*
* Otherwise, sleep with millisecond precision. Clamp the
* time to ~4 seconds (0xFFF/1000 => 4.09 seconds).
*
* Note that the do_div() macro divides the first argument in
* place.
*/
int msec;
NvU64 tmp = usec + 500;
do_div(tmp, 1000);
msec = (int) (tmp & 0xFFF);
/*
* XXX NVKMS TODO: this may need to be msleep_interruptible(),
* though the callers would need to be made to handle
* returning early.
*/
msleep(msec);
}
}
NvU64 nvkms_get_usec(void)
{
struct timespec64 ts;
NvU64 ns;
ktime_get_raw_ts64(&ts);
ns = timespec64_to_ns(&ts);
return ns / 1000;
}
int nvkms_copyin(void *kptr, NvU64 uaddr, size_t n)
{
if (!nvKmsNvU64AddressIsSafe(uaddr)) {
return -EINVAL;
}
if (copy_from_user(kptr, nvKmsNvU64ToPointer(uaddr), n) != 0) {
return -EFAULT;
}
return 0;
}
int nvkms_copyout(NvU64 uaddr, const void *kptr, size_t n)
{
if (!nvKmsNvU64AddressIsSafe(uaddr)) {
return -EINVAL;
}
if (copy_to_user(nvKmsNvU64ToPointer(uaddr), kptr, n) != 0) {
return -EFAULT;
}
return 0;
}
void nvkms_yield(void)
{
schedule();
}
void nvkms_dump_stack(void)
{
dump_stack();
}
int nvkms_snprintf(char *str, size_t size, const char *format, ...)
{
int ret;
va_list ap;
va_start(ap, format);
ret = vsnprintf(str, size, format, ap);
va_end(ap);
return ret;
}
int nvkms_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
return vsnprintf(str, size, format, ap);
}
void nvkms_log(const int level, const char *gpuPrefix, const char *msg)
{
const char *levelString;
const char *levelPrefix;
switch (level) {
default:
case NVKMS_LOG_LEVEL_INFO:
levelPrefix = "";
levelString = KERN_INFO;
break;
case NVKMS_LOG_LEVEL_WARN:
levelPrefix = "WARNING: ";
levelString = KERN_WARNING;
break;
case NVKMS_LOG_LEVEL_ERROR:
levelPrefix = "ERROR: ";
levelString = KERN_ERR;
break;
}
printk("%s%s%s%s%s\n",
levelString, NVKMS_LOG_PREFIX, levelPrefix, gpuPrefix, msg);
}
void
nvkms_event_queue_changed(nvkms_per_open_handle_t *pOpenKernel,
NvBool eventsAvailable)
{
struct nvkms_per_open *popen = pOpenKernel;
switch (popen->type) {
case NVKMS_CLIENT_USER_SPACE:
/*
* Write popen->events.available atomically, to avoid any races or
* memory barrier issues interacting with nvkms_poll().
*/
atomic_set(&popen->u.user.events.available, eventsAvailable);
wake_up_interruptible(&popen->u.user.events.wait_queue);
break;
case NVKMS_CLIENT_KERNEL_SPACE:
if (eventsAvailable) {
nv_kthread_q_schedule_q_item(
&nvkms_kthread_q,
&popen->u.kernel.events.nv_kthread_q_item);
}
break;
}
}
static void nvkms_suspend(NvU32 gpuId)
{
nvKmsKapiSuspendResume(NV_TRUE /* suspend */);
if (gpuId == 0) {
nvkms_write_lock_pm_lock();
}
down(&nvkms_lock);
nvKmsSuspend(gpuId);
up(&nvkms_lock);
}
static void nvkms_resume(NvU32 gpuId)
{
down(&nvkms_lock);
nvKmsResume(gpuId);
up(&nvkms_lock);
if (gpuId == 0) {
nvkms_write_unlock_pm_lock();
}
nvKmsKapiSuspendResume(NV_FALSE /* suspend */);
}
static void nvkms_remove(NvU32 gpuId)
{
nvKmsKapiRemove(gpuId);
// Eventually, this function should also terminate all NVKMS clients and
// free the NVDevEvoRec. Until that is implemented, all NVKMS clients must
// be closed before a device is removed.
}
static void nvkms_probe(const nv_gpu_info_t *gpu_info)
{
nvKmsKapiProbe(gpu_info);
}
/*************************************************************************
* Interface with resman.
*************************************************************************/
static nvidia_modeset_rm_ops_t __rm_ops = { 0 };
static nvidia_modeset_callbacks_t nvkms_rm_callbacks = {
.suspend = nvkms_suspend,
.resume = nvkms_resume,
.remove = nvkms_remove,
.probe = nvkms_probe,
};
static int nvkms_alloc_rm(void)
{
NV_STATUS nvstatus;
int ret;
__rm_ops.version_string = NV_VERSION_STRING;
nvstatus = nvidia_get_rm_ops(&__rm_ops);
if (nvstatus != NV_OK) {
printk(KERN_ERR NVKMS_LOG_PREFIX "Version mismatch: "
"nvidia.ko(%s) nvidia-modeset.ko(%s)\n",
__rm_ops.version_string, NV_VERSION_STRING);
return -EINVAL;
}
ret = __rm_ops.set_callbacks(&nvkms_rm_callbacks);
if (ret < 0) {
printk(KERN_ERR NVKMS_LOG_PREFIX "Failed to register callbacks\n");
return ret;
}
return 0;
}
static void nvkms_free_rm(void)
{
__rm_ops.set_callbacks(NULL);
}
void nvkms_call_rm(void *ops)
{
nvidia_modeset_stack_ptr stack = NULL;
if (__rm_ops.alloc_stack(&stack) != 0) {
return;
}
__rm_ops.op(stack, ops);
__rm_ops.free_stack(stack);
}
/*************************************************************************
* ref_ptr implementation.
*************************************************************************/
struct nvkms_ref_ptr {
nv_kref_t refcnt;
// Access to ptr is guarded by the nvkms_lock.
void *ptr;
};
struct nvkms_ref_ptr* nvkms_alloc_ref_ptr(void *ptr)
{
struct nvkms_ref_ptr *ref_ptr = nvkms_alloc(sizeof(*ref_ptr), NV_FALSE);
if (ref_ptr) {
// The ref_ptr owner counts as a reference on the ref_ptr itself.
nv_kref_init(&ref_ptr->refcnt);
ref_ptr->ptr = ptr;
}
return ref_ptr;
}
void nvkms_free_ref_ptr(struct nvkms_ref_ptr *ref_ptr)
{
if (ref_ptr) {
ref_ptr->ptr = NULL;
// Release the owner's reference of the ref_ptr.
nvkms_dec_ref(ref_ptr);
}
}
void nvkms_inc_ref(struct nvkms_ref_ptr *ref_ptr)
{
nv_kref_get(&ref_ptr->refcnt);
}
static void ref_ptr_free(nv_kref_t *ref)
{
struct nvkms_ref_ptr *ref_ptr = container_of(ref, struct nvkms_ref_ptr,
refcnt);
nvkms_free(ref_ptr, sizeof(*ref_ptr));
}
void* nvkms_dec_ref(struct nvkms_ref_ptr *ref_ptr)
{
void *ptr = ref_ptr->ptr;
nv_kref_put(&ref_ptr->refcnt, ref_ptr_free);
return ptr;
}
/*************************************************************************
* Timer support
*
* Core NVKMS needs to be able to schedule work to execute in the
* future, within process context.
*
* To achieve this, use struct timer_list to schedule a timer
* callback, nvkms_timer_callback(). This will execute in softirq
* context, so from there schedule an nv_kthread_q item,
* nvkms_kthread_q_callback(), which will execute in process context.
*************************************************************************/
struct nvkms_timer_t {
nv_kthread_q_item_t nv_kthread_q_item;
struct timer_list kernel_timer;
NvBool cancel;
NvBool complete;
NvBool isRefPtr;
NvBool kernel_timer_created;
nvkms_timer_proc_t *proc;
void *dataPtr;
NvU32 dataU32;
struct list_head timers_list;
};
/*
* Global list with pending timers, any change requires acquiring lock
*/
static struct {
spinlock_t lock;
struct list_head list;
} nvkms_timers;
static void nvkms_kthread_q_callback(void *arg)
{
struct nvkms_timer_t *timer = arg;
void *dataPtr;
unsigned long flags = 0;
/*
* We can delete this timer from pending timers list - it's being
* processed now.
*/
spin_lock_irqsave(&nvkms_timers.lock, flags);
list_del(&timer->timers_list);
spin_unlock_irqrestore(&nvkms_timers.lock, flags);
/*
* After kthread_q_callback we want to be sure that timer_callback
* for this timer also have finished. It's important during module
* unload - this way we can safely unload this module by first deleting
* pending timers and than waiting for workqueue callbacks.
*/
if (timer->kernel_timer_created) {
nv_timer_delete_sync(&timer->kernel_timer);