/
nvidia-modeset-freebsd.c
1395 lines (1144 loc) · 36.8 KB
/
nvidia-modeset-freebsd.c
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
/* _NVRM_COPYRIGHT_BEGIN_
*
* Copyright 2015 by NVIDIA Corporation. All rights reserved. All
* information contained herein is proprietary and confidential to NVIDIA
* Corporation. Any use, reproduction, or disclosure without the written
* permission of NVIDIA Corporation is prohibited.
*
* _NVRM_COPYRIGHT_END_
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/module.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/ioccom.h>
#include <sys/libkern.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sx.h>
#include <sys/conf.h>
#include <sys/syslog.h>
#include <sys/taskqueue.h>
#include <sys/selinfo.h>
#include <sys/poll.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/stack.h>
#include <sys/sysproto.h>
#include "nvkms-ioctl.h"
#include "nvidia-modeset-os-interface.h"
#include "nvkms.h"
#include "nv-modeset-interface.h"
#include "nv-retpoline.h"
/*
* This option decides if nvidia-modeset.ko will be built with support
* for Linux or Linux 32-bit (FreeBSD/amd64) compatibility. This
* makes nvidia-modeset.ko dependent on linux.ko; if you don't need
* Linux compatibility, then you can safely unset this flag.
*/
#define NVKMS_SUPPORT_LINUX_COMPAT
#if defined(NVKMS_SUPPORT_LINUX_COMPAT)
#if defined(NVCPU_X86)
#include "machine/../linux/linux.h"
#include "machine/../linux/linux_proto.h"
#elif defined(NVCPU_X86_64)
#include "machine/../linux32/linux.h"
#include "machine/../linux32/linux32_proto.h"
#endif
#include <compat/linux/linux_ioctl.h>
#include <compat/linux/linux_util.h>
#endif
#define NVKMS_LOG_PREFIX "nvidia-modeset: "
#define NVKMS_CDEV_MINOR 254
#define NVKMS_USECS_TO_TICKS(usec) ((usec) * hz / 1000000)
MALLOC_DEFINE(M_NVIDIA_MODESET,
"nvidia-modeset", "NVIDIA modeset memory allocations");
static void nvkms_close(void *arg);
/*************************************************************************
* NVKMS uses a global lock, nvkms_lock. The lock is taken in the
* file operation callback functions when calling into core NVKMS.
*************************************************************************/
static struct sx nvkms_lock;
/*************************************************************************
* NVKMS uses a global clients counter and boolean unloading flag. This is
* used to determine whether module can be safely unloaded.
*************************************************************************/
static struct {
struct sx lock;
unsigned int client_counter;
NvBool is_unloading;
} nvkms_module;
/*************************************************************************
* The nvkms_per_open structure tracks data that is specific to a
* single file open./
*************************************************************************/
/*
* Look here, we have two event queues that are totally out of sync. Maybe
* work is getting submitted in two different ways and isn't serialized.
* Make this finally shared code to get rid of all differences.
*
* These should be a union just like in the linux code
*/
struct nvkms_per_open {
void *data;
enum NvKmsClientType type;
struct mtx lock;
union {
struct {
uint32_t available;
struct selinfo select;
} user;
struct {
nvkms_timer_handle_t *task;
struct NvKmsKapiDevice *device;
} kernel;
};
};
NvBool nvkms_output_rounding_fix(void)
{
return NV_TRUE;
}
NvBool nvkms_disable_vrr_memclk_switch(void)
{
return NV_FALSE;
}
/*************************************************************************
* nvidia-modeset-os-interface.h functions. It is assumed that these
* are called while nvkms_lock is held.
*************************************************************************/
void* nvkms_alloc(size_t size, NvBool zero)
{
return malloc(size, M_NVIDIA_MODESET, M_WAITOK | (zero ? M_ZERO : 0));
}
void nvkms_free(void *ptr, size_t size)
{
free(ptr, M_NVIDIA_MODESET);
}
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)
{
DELAY(usec);
}
NvU64 nvkms_get_usec(void)
{
struct timeval tv;
getmicrotime(&tv);
return (((NvU64)tv.tv_sec) * 1000000) + tv.tv_usec;
}
int nvkms_copyin(void *kptr, NvU64 uaddr, size_t n)
{
if (!nvKmsNvU64AddressIsSafe(uaddr)) {
return EINVAL;
}
return copyin(nvKmsNvU64ToPointer(uaddr), kptr, n);
}
int nvkms_copyout(NvU64 uaddr, const void *kptr, size_t n)
{
if (!nvKmsNvU64AddressIsSafe(uaddr)) {
return EINVAL;
}
return copyout(kptr, nvKmsNvU64ToPointer(uaddr), n);
}
void nvkms_yield(void)
{
pause("yield", 1 /* timeout in 1/hz units */);
}
void nvkms_dump_stack(void)
{
struct stack *st = stack_create(0);
stack_save(st);
stack_print(st);
stack_destroy(st);
}
/* Unsupported STUB for nvkms_syncpt_op APIs */
NvBool nvkms_syncpt_op(
enum NvKmsSyncPtOp op,
NvKmsSyncPtOpParams *params)
{
return NV_FALSE;
}
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)
{
int priority;
const char *levelPrefix;
switch (level) {
default:
case NVKMS_LOG_LEVEL_INFO:
levelPrefix = "";
priority = LOG_INFO;
break;
case NVKMS_LOG_LEVEL_WARN:
levelPrefix = "WARNING: ";
priority = LOG_WARNING;
break;
case NVKMS_LOG_LEVEL_ERROR:
levelPrefix = "ERROR: ";
priority = LOG_ERR;
break;
}
log(priority, "%s%s%s%s\n", NVKMS_LOG_PREFIX, levelPrefix, gpuPrefix, msg);
}
/*************************************************************************
* ref_ptr implementation.
*************************************************************************/
struct nvkms_ref_ptr {
struct mtx lock;
int refcnt;
// Access to ptr is guarded by the nvkms_lock.
void *ptr;
};
struct nvkms_ref_ptr* nvkms_alloc_ref_ptr(void *ptr)
{
/*
* Initialize memory to avoid spurious "lock re-initialization" errors.
* A little more detail can be found in the FreeBSD PR 201340 starting around
* comment #50.
*/
struct nvkms_ref_ptr *ref_ptr = nvkms_alloc(sizeof(*ref_ptr), NV_TRUE);
if (ref_ptr) {
mtx_init(&ref_ptr->lock, "nvkms-ref-ptr-lock", NULL, MTX_SPIN);
// The ref_ptr owner counts as a reference on the ref_ptr itself.
ref_ptr->refcnt = 1;
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)
{
mtx_lock_spin(&ref_ptr->lock);
ref_ptr->refcnt++;
mtx_unlock_spin(&ref_ptr->lock);
}
void* nvkms_dec_ref(struct nvkms_ref_ptr *ref_ptr)
{
void *ptr = ref_ptr->ptr;
mtx_lock_spin(&ref_ptr->lock);
if (--ref_ptr->refcnt == 0) {
mtx_destroy(&ref_ptr->lock);
nvkms_free(ref_ptr, sizeof(*ref_ptr));
} else {
mtx_unlock_spin(&ref_ptr->lock);
}
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 the 'callout' mechanism to schedule a
* callback, nvkms_callout_callback(). This will execute in softirq
* context, so from there schedule a taskqueue task,
* nvkms_taskqueue_callback(), which will execute in process context.
*
* This could be simpler with the taskqueue_enqueue_timeout(9) family
* of functions to defer work until a minimum period of time has
* passed. Unfortunately, the taskqueue_enqueue_timeout(9) interface
* is only available on FreeBSD 9.0 and higher.
*
* References:
*
* callout_reset(9)
* taskqueue_enqueue(9)
* https://www.freebsd.org/doc/en_US.ISO8859-1/books/arch-handbook/smp-design.html
* https://svnweb.freebsd.org/base?view=revision&revision=221059
*************************************************************************/
/*
* Older versions of FreeBSD before 27f09959d5f507465cc7f202c1b34987f0cdee55 do
* not have a working TASKQUEUE_FAST_DEFINE_THREAD, so we explicitly use
* TASKQUEUE_FAST_DEFINE instead.
*/
TASKQUEUE_FAST_DEFINE(nvkms, taskqueue_thread_enqueue, \
&taskqueue_nvkms, taskqueue_start_threads(&taskqueue_nvkms, \
1, PWAIT, "%s taskq", "nvkms"));
struct nvkms_timer_t {
struct callout callout;
struct task task;
NvBool cancel;
NvBool complete;
NvBool isRefPtr;
NvBool needsNvkmsLock;
NvBool callout_created;
nvkms_timer_proc_t *proc;
void *dataPtr;
NvU32 dataU32;
LIST_ENTRY(nvkms_timer_t) timers_list;
};
/*
* Global list with pending timers, any change requires acquiring lock
*/
static struct {
struct mtx lock;
LIST_HEAD(nvkms_timers_head, nvkms_timer_t) list;
} nvkms_timers;
static void nvkms_taskqueue_callback(void *arg, int pending)
{
struct nvkms_timer_t *timer = arg;
void *dataPtr;
/*
* We can delete this timer from pending timers list - it's being
* processed now.
*/
mtx_lock_spin(&nvkms_timers.lock);
LIST_REMOVE(timer, timers_list);
mtx_unlock_spin(&nvkms_timers.lock);
/*
* After taskqueue_callback we want to be sure that callout_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 taskqueue callbacks.
*/
if (timer->callout_created) {
callout_drain(&timer->callout);
}
/*
* Only lock if this timer requests it. DRM's callback nv_drm_event_callback
* will not need this, since it may reenter nvkms through the kapi and lock
* nvkms_lock then.
*/
if (timer->needsNvkmsLock) {
sx_xlock(&nvkms_lock);
}
if (timer->isRefPtr) {
// If the object this timer refers to was destroyed, treat the timer as
// canceled.
dataPtr = nvkms_dec_ref(timer->dataPtr);
if (!dataPtr) {
timer->cancel = NV_TRUE;
}
} else {
dataPtr = timer->dataPtr;
}
if (!timer->cancel) {
timer->proc(dataPtr, timer->dataU32);
timer->complete = NV_TRUE;
}
if (timer->needsNvkmsLock) {
sx_xunlock(&nvkms_lock);
}
if (timer->cancel || timer->isRefPtr) {
nvkms_free(timer, sizeof(*timer));
}
}
static void nvkms_callout_callback(void *arg)
{
struct nvkms_timer_t *timer = arg;
/* In softirq context, so schedule nvkms_taskqueue_callback(). */
taskqueue_enqueue(taskqueue_nvkms, &timer->task);
}
static void
nvkms_init_timer(struct nvkms_timer_t *timer, nvkms_timer_proc_t *proc,
void *dataPtr, NvU32 dataU32, NvBool isRefPtr, NvU64 usec,
NvBool needsNvkmsLock)
{
timer->cancel = NV_FALSE;
timer->complete = NV_FALSE;
timer->isRefPtr = isRefPtr;
timer->needsNvkmsLock = needsNvkmsLock;
timer->proc = proc;
timer->dataPtr = dataPtr;
timer->dataU32 = dataU32;
TASK_INIT(&timer->task,
0 /* priority */,
nvkms_taskqueue_callback, (void *)timer);
/*
* After adding timer to timers_list we need to finish referencing it
* (calling taskqueue_enqueue() or callout_reset()) before releasing
* the lock. Otherwise, if the code to free the timer were ever updated to
* run in parallel with this, it could race against nvkms_init_timer()
* and free the timer before its initialization is complete.
*/
mtx_lock_spin(&nvkms_timers.lock);
LIST_INSERT_HEAD(&nvkms_timers.list, timer, timers_list);
if (usec == 0) {
timer->callout_created = NV_FALSE;
taskqueue_enqueue(taskqueue_nvkms, &timer->task);
} else {
/* CALLOUT_MPSAFE means that the callout handler is SMP-safe. */
callout_init(&timer->callout, CALLOUT_MPSAFE);
timer->callout_created = NV_TRUE;
callout_reset(&timer->callout,
NVKMS_USECS_TO_TICKS(usec),
nvkms_callout_callback, (void *) timer);
}
mtx_unlock_spin(&nvkms_timers.lock);
}
static nvkms_timer_handle_t*
nvkms_alloc_timer_locked(nvkms_timer_proc_t *proc,
void *dataPtr, NvU32 dataU32,
NvU64 usec, NvBool needsNvkmsLock)
{
// nvkms_alloc_timer cannot be called from an interrupt context.
struct nvkms_timer_t *timer = nvkms_alloc(sizeof(*timer), NV_TRUE);
if (timer) {
nvkms_init_timer(timer, proc, dataPtr, dataU32, NV_FALSE, usec, needsNvkmsLock);
}
return timer;
}
nvkms_timer_handle_t*
nvkms_alloc_timer(nvkms_timer_proc_t *proc,
void *dataPtr, NvU32 dataU32,
NvU64 usec)
{
return nvkms_alloc_timer_locked(proc, dataPtr, dataU32, usec, NV_TRUE);
}
NvBool
nvkms_alloc_timer_with_ref_ptr(nvkms_timer_proc_t *proc,
struct nvkms_ref_ptr *ref_ptr,
NvU32 dataU32, NvU64 usec)
{
// nvkms_alloc_timer_with_ref_ptr is called from an interrupt bottom half
// handler.
// TODO: Determine whether we really need to use M_NOWAIT here.
struct nvkms_timer_t *timer = malloc(sizeof(*timer), M_NVIDIA_MODESET,
M_NOWAIT | M_ZERO);
if (timer) {
// Reference the ref_ptr to make sure that it doesn't get freed before
// the timer fires.
nvkms_inc_ref(ref_ptr);
nvkms_init_timer(timer, proc, ref_ptr, dataU32, NV_TRUE, usec, NV_TRUE);
}
return timer != NULL;
}
void nvkms_free_timer(nvkms_timer_handle_t *handle)
{
struct nvkms_timer_t *timer = handle;
if (timer == NULL) {
return;
}
if (timer->complete) {
nvkms_free(timer, sizeof(*timer));
return;
}
timer->cancel = NV_TRUE;
}
static void nvkms_suspend(NvU32 gpuId)
{
sx_xlock(&nvkms_lock);
nvKmsSuspend(gpuId);
sx_xunlock(&nvkms_lock);
}
static void nvkms_resume(NvU32 gpuId)
{
sx_xlock(&nvkms_lock);
nvKmsResume(gpuId);
sx_xunlock(&nvkms_lock);
}
static void nvkms_kapi_task_callback(void *arg, NvU32 dataU32 __unused)
{
struct nvkms_per_open *popen = arg;
struct NvKmsKapiDevice *device = popen->kernel.device;
nvkms_free_timer(popen->kernel.task);
popen->kernel.task = NULL;
nvKmsKapiHandleEventQueueChange(device);
}
void
nvkms_event_queue_changed(nvkms_per_open_handle_t *pOpenKernel,
NvBool eventsAvailable)
{
struct nvkms_per_open *popen = pOpenKernel;
mtx_lock(&popen->lock);
switch (popen->type) {
case NVKMS_CLIENT_USER_SPACE:
popen->user.available = eventsAvailable;
selwakeup(&popen->user.select);
break;
case NVKMS_CLIENT_KERNEL_SPACE:
if (!popen->kernel.task) {
popen->kernel.task = nvkms_alloc_timer_locked(nvkms_kapi_task_callback,
popen,
0, /* dataU32 */
0, /* callout delay */
NV_FALSE);
}
break;
}
mtx_unlock(&popen->lock);
}
/*************************************************************************
* Interface with resman.
*
* Due to the global nvkms_lock, all NVKMS calls to RM are serialized,
* so we can use a single nvidia_modeset_stack_ptr for calling RM.
*************************************************************************/
static nvidia_modeset_rm_ops_t __rm_ops = { 0 };
static nvidia_modeset_callbacks_t nvkms_rm_callbacks = {
nvkms_suspend,
nvkms_resume
};
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) {
printf(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) {
printf(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);
}
#include <sys/caprights.h>
#include <sys/capsicum.h>
/*
* There doesn't appear to be a clean API to retrieve the cdevpriv (as
* set by devfs_set_cdevpriv(9)) of an arbitrary 'struct file', so
* reach into the cdev_privdata.
*
* It would be nice if the kernel provided something like
* devfs_get_cdevpriv_from_file() as proposed here:
*
* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=201611
*/
#define NV_KERNEL_HAS_DEVFS_GET_CDEVPRIV_FROM_FILE 0
#if !NV_KERNEL_HAS_DEVFS_GET_CDEVPRIV_FROM_FILE
#include <fs/devfs/devfs_int.h>
#endif
void* nvkms_get_per_open_data(int fd)
{
struct file *fp = NULL;
struct nvkms_per_open *popen = NULL;
int status;
void *data = NULL;
cap_rights_t rights;
status = fget(curthread, fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
if (status != 0) {
return NULL;
}
#if NV_KERNEL_HAS_DEVFS_GET_CDEVPRIV_FROM_FILE
status = devfs_get_cdevpriv_from_file(&popen, fp);
if (status != 0) {
goto done;
}
#else
{
struct cdev_privdata *p = fp->f_cdevpriv;
/*
* devfs_set_cdevpriv() assigns nvkms_close to
* cdev_privdata::cdpd_dtr if this is an nvidia-modeset struct
* file.
*/
if ((p == NULL) || (p->cdpd_dtr != nvkms_close)) {
goto done;
}
popen = p->cdpd_data;
}
#endif
if (popen == NULL) {
goto done;
}
data = popen->data;
done:
/*
* If we reach here, fp is a valid struct file pointer, returned
* by fget(9).
*
* fget(9) incremented the struct file's reference count, which
* needs to be balanced with a call to fdrop(9). It is safe to
* decrement the reference count before returning the cdevpriv
* value because core NVKMS is currently holding the nvkms_lock,
* which prevents the nvkms_close() => nvKmsClose() call chain
* from freeing the file out from under the caller of
* nvkms_get_per_open_data().
*/
fdrop(fp, curthread);
return data;
}
NvBool nvkms_fd_is_nvidia_chardev(int fd)
{
/*
* Currently, freebsd doesn't have a case where we would expect
* fd to be non RM fd.
*/
return NV_TRUE;
}
NvBool nvkms_open_gpu(NvU32 gpuId)
{
nvidia_modeset_stack_ptr stack = NULL;
NvBool ret;
if (__rm_ops.alloc_stack(&stack) != 0) {
return NV_FALSE;
}
ret = __rm_ops.open_gpu(gpuId, stack) == 0;
__rm_ops.free_stack(stack);
return ret;
}
void nvkms_close_gpu(NvU32 gpuId)
{
nvidia_modeset_stack_ptr stack = NULL;
if (__rm_ops.alloc_stack(&stack) != 0) {
return;
}
__rm_ops.close_gpu(gpuId, stack);
__rm_ops.free_stack(stack);
}
NvU32 nvkms_enumerate_gpus(nv_gpu_info_t *gpu_info)
{
return 0;
}
NvBool nvkms_allow_write_combining(void)
{
return __rm_ops.system_info.allow_write_combining;
}
/*************************************************************************
* Implementation of sysfs interface to control backlight
*************************************************************************/
struct nvkms_backlight_device*
nvkms_register_backlight(NvU32 gpu_id, NvU32 display_id, void *drv_priv,
NvU32 current_brightness)
{
return NULL;
}
void nvkms_unregister_backlight(struct nvkms_backlight_device *nvkms_bd)
{
}
/*************************************************************************
* Common to both user-space and kapi NVKMS interfaces
*************************************************************************/
/*
* a mirror of nvkms_open. Does the kernel space opening
* - don't add character device
* - don't handle NVKMS_CLIENT_USER_SPACE (thats nvkms_open)
* - doesn't do select, uses task queueing
*/
static struct nvkms_per_open *nvkms_open_common(enum NvKmsClientType type,
struct NvKmsKapiDevice *device,
int *status)
{
struct nvkms_per_open *popen = NULL;
popen = nvkms_alloc(sizeof(*popen), NV_TRUE);
if (popen == NULL) {
*status = ENOMEM;
goto failed;
}
popen->type = type;
sx_xlock(&nvkms_module.lock);
if (nvkms_module.is_unloading) {
sx_xunlock(&nvkms_module.lock);
*status = ENXIO;
goto failed;
}
nvkms_module.client_counter += 1;
sx_xunlock(&nvkms_module.lock);
mtx_init(&popen->lock, "nvkms-events-lock", NULL, 0);
switch (type) {
case NVKMS_CLIENT_USER_SPACE:
break;
case NVKMS_CLIENT_KERNEL_SPACE:
/* enqueue our new task */
popen->kernel.device = device;
popen->kernel.task = nvkms_alloc_timer_locked(nvkms_kapi_task_callback,
popen,
0, /* dataU32 */
0, /* callout delay */
NV_FALSE);
break;
}
sx_xlock(&nvkms_lock);
popen->data = nvKmsOpen(curproc->p_pid, type, popen);
sx_xunlock(&nvkms_lock);
if (popen->data == NULL) {
*status = EPERM;
goto failed;
}
*status = 0;
return popen;
failed:
nvkms_free(popen, sizeof(*popen));
return NULL;
}
static void nvkms_close_common(struct nvkms_per_open *popen)
{
sx_xlock(&nvkms_lock);
nvKmsClose(popen->data);
sx_xunlock(&nvkms_lock);
mtx_destroy(&popen->lock);
switch (popen->type) {
case NVKMS_CLIENT_USER_SPACE:
break;
case NVKMS_CLIENT_KERNEL_SPACE:
/*
* Flush any outstanding nvkms_kapi_task_callback() work
* items before freeing popen.
*
* Note that this must be done after the above nvKmsClose() call, to
* guarantee that no more nvkms_kapi_task_callback() work
* items get scheduled.
*
* Also, note that though popen->data is freed above, any subsequent
* nvkms_kapi_task_callback()'s for this popen should be
* safe: if any nvkms_kapi_task_callback()-initiated work
* attempts to call back into NVKMS, the popen->data==NULL check in
* nvkms_ioctl_common() should reject the request.
*/
nvkms_free_timer(popen->kernel.task);
popen->kernel.task = NULL;
break;
}
nvkms_free(popen, sizeof(*popen));
sx_xlock(&nvkms_module.lock);
nvkms_module.client_counter -= 1;
sx_xunlock(&nvkms_module.lock);
}
static int nvkms_ioctl_common
(
struct nvkms_per_open *popen,
NvU32 cmd, NvU64 address, const size_t size
)
{
NvBool ret;
sx_xlock(&nvkms_lock);
if (popen && popen->data) {
ret = nvKmsIoctl(popen->data, cmd, address, size);
} else {
ret = NV_FALSE;
}
sx_xunlock(&nvkms_lock);
return ret ? 0 : EPERM;
}
/*************************************************************************
* NVKMS interface for kernel space NVKMS clients like KAPI
*************************************************************************/
struct nvkms_per_open* nvkms_open_from_kapi
(
struct NvKmsKapiDevice *device
)
{
int status = 0;
return nvkms_open_common(NVKMS_CLIENT_KERNEL_SPACE, device, &status);
}
void nvkms_close_from_kapi(struct nvkms_per_open *popen)
{
nvkms_close_common(popen);
}
NvBool nvkms_ioctl_from_kapi
(
struct nvkms_per_open *popen,
NvU32 cmd, void *params_address, const size_t params_size
)
{
return nvkms_ioctl_common(popen, cmd,
(NvU64)(NvUPtr)params_address, params_size) == 0;
}
/*************************************************************************
* APIs for locking.
*************************************************************************/
/* according to man mutexes on bsd are faster than semaphores */
struct nvkms_sema_t {
struct mtx nvs_mutex;
};
nvkms_sema_handle_t* nvkms_sema_alloc(void)
{
nvkms_sema_handle_t *sema = nvkms_alloc(sizeof(nvkms_sema_handle_t), NV_TRUE);
if (sema) {
mtx_init(&(sema->nvs_mutex), "NVIDIA Mutex", NULL, MTX_DEF);
}
return sema;
}
void nvkms_sema_free(nvkms_sema_handle_t *sema)
{
mtx_destroy(&sema->nvs_mutex);
nvkms_free(sema, sizeof(*sema));
}
void nvkms_sema_down(nvkms_sema_handle_t *sema)
{
mtx_lock(&sema->nvs_mutex);
}
void nvkms_sema_up(nvkms_sema_handle_t *sema)
{
mtx_unlock(&sema->nvs_mutex);
}
/*************************************************************************
* NVKMS KAPI functions
************************************************************************/