-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmods_krnl.c
More file actions
2918 lines (2399 loc) · 67.1 KB
/
mods_krnl.c
File metadata and controls
2918 lines (2399 loc) · 67.1 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-License-Identifier: GPL-2.0-only
/* SPDX-FileCopyrightText: Copyright (c) 2008-2025, NVIDIA CORPORATION. All rights reserved. */
#include "mods_internal.h"
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/pagemap.h>
#include <linux/poll.h>
#include <linux/random.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#ifdef MODS_HAS_CONSOLE_LOCK
# include <linux/console.h>
# include <linux/kd.h>
# include <linux/tty.h>
# include <linux/console_struct.h>
# include <linux/vt_kern.h>
#endif
#ifdef CONFIG_X86
# include <linux/screen_info.h>
# include <asm/msr.h>
#endif
/***********************************************************************
* mods_krnl_* functions, driver interfaces called by the Linux kernel *
***********************************************************************/
static int mods_krnl_open(struct inode *, struct file *);
static int mods_krnl_close(struct inode *, struct file *);
static POLL_TYPE mods_krnl_poll(struct file *, poll_table *);
static int mods_krnl_mmap(struct file *, struct vm_area_struct *);
static long mods_krnl_ioctl(struct file *, unsigned int, unsigned long);
/* character driver entry points */
static const struct file_operations mods_fops = {
.owner = THIS_MODULE,
.open = mods_krnl_open,
.release = mods_krnl_close,
.poll = mods_krnl_poll,
.mmap = mods_krnl_mmap,
.unlocked_ioctl = mods_krnl_ioctl,
#if defined(HAVE_COMPAT_IOCTL)
.compat_ioctl = mods_krnl_ioctl,
#endif
};
#define DEVICE_NAME "mods"
static struct miscdevice mods_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &mods_fops
};
#if defined(CONFIG_PCI)
static pci_ers_result_t mods_pci_error_detected(struct pci_dev *,
pci_channel_state_t);
static pci_ers_result_t mods_pci_mmio_enabled(struct pci_dev *);
static void mods_pci_resume(struct pci_dev *);
static struct pci_error_handlers mods_pci_error_handlers = {
.error_detected = mods_pci_error_detected,
.mmio_enabled = mods_pci_mmio_enabled,
.resume = mods_pci_resume,
};
static const struct pci_device_id mods_pci_table[] = {
{
.vendor = PCI_VENDOR_ID_NVIDIA,
.device = PCI_ANY_ID,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
.class = (PCI_CLASS_DISPLAY_VGA << 8),
.class_mask = ~0
},
{
.vendor = PCI_VENDOR_ID_NVIDIA,
.device = PCI_ANY_ID,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
.class = (PCI_CLASS_DISPLAY_3D << 8),
.class_mask = ~0
},
{
.vendor = PCI_VENDOR_ID_NVIDIA,
.device = PCI_ANY_ID,
.subvendor = PCI_ANY_ID,
.subdevice = PCI_ANY_ID,
.class = (PCI_CLASS_BRIDGE_OTHER << 8),
.class_mask = ~0
},
{ 0 }
};
static int mods_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
struct en_dev_entry *dpriv;
dpriv = kzalloc(sizeof(*dpriv), GFP_KERNEL | __GFP_NORETRY);
if (unlikely(!dpriv))
return -ENOMEM;
dpriv->dev = pci_dev_get(dev);
#if defined(MODS_HAS_ARM_SMCCC)
{
int err = 0;
int qm = mods_get_force_qual_mode();
if (qm) {
qm &= (MODS_QUAL_MODE_LINK_IDE | MODS_QUAL_MODE_C2C | MODS_QUAL_MODE_SEL_IDE);
mods_info_printk("enabling qual mode %u on dev %04x:%02x:%02x.%x\n",
qm,
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn));
err = mods_qdev_enable(dev, qm, &dpriv->qual_dev);
if (unlikely(err)) {
mods_error_printk("failed to enable qual mode\n");
pci_dev_put(dev);
kfree(dpriv);
return err;
}
}
}
#endif
init_completion(&dpriv->client_completion);
pci_set_drvdata(dev, dpriv);
mods_debug_printk(DEBUG_PCI,
"probed dev %04x:%02x:%02x.%x vendor %04x device %04x\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dev->vendor, dev->device);
return 0;
}
static void mods_pci_remove(struct pci_dev *dev)
{
struct en_dev_entry *dpriv = pci_get_drvdata(dev);
WARN_ON(!dpriv);
while (true) {
mutex_lock(mods_get_irq_mutex());
if (!is_valid_client_id(dpriv->client_id))
break;
mods_info_printk("removing dev %04x:%02x:%02x.%x, waiting for client %u\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dpriv->client_id);
mutex_unlock(mods_get_irq_mutex());
wait_for_completion(&dpriv->client_completion);
}
#if defined(MODS_HAS_ARM_SMCCC)
if (dpriv->qual_dev) {
mods_info_printk("disabling qual mode on dev %04x:%02x:%02x.%x\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn));
mods_qdev_disable(dpriv->qual_dev);
dpriv->qual_dev = NULL;
}
#endif
pci_dev_put(dpriv->dev);
pci_set_drvdata(dev, NULL);
kfree(dpriv);
mutex_unlock(mods_get_irq_mutex());
mods_debug_printk(DEBUG_PCI,
"removed dev %04x:%02x:%02x.%x vendor %04x device %04x\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dev->vendor, dev->device);
}
#if defined(CONFIG_PCI) && defined(MODS_HAS_SRIOV)
static int mods_pci_sriov_configure(struct pci_dev *dev, int numvfs);
#endif
static struct pci_driver mods_pci_driver = {
.name = DEVICE_NAME,
.id_table = mods_pci_table,
.probe = mods_pci_probe,
.remove = mods_pci_remove,
.err_handler = &mods_pci_error_handlers,
#ifdef MODS_HAS_SRIOV
.sriov_configure = mods_pci_sriov_configure,
#endif
};
#endif
/***********************************************
* module wide parameters and access functions *
* used to avoid globalization of variables *
***********************************************/
#ifdef MODS_HAS_TEGRA
# define MODS_MULTI_INSTANCE_DEFAULT_VALUE 1
#else
# define MODS_MULTI_INSTANCE_DEFAULT_VALUE 0
#endif
static int debug;
static int multi_instance = MODS_MULTI_INSTANCE_DEFAULT_VALUE;
static u32 access_token = MODS_ACCESS_TOKEN_NONE;
static int force_qual_mode;
#if defined(CONFIG_PCI) && defined(MODS_HAS_SRIOV)
static int mods_pci_sriov_configure(struct pci_dev *dev, int numvfs)
{
int totalvfs;
int err = 0;
LOG_ENT();
totalvfs = pci_sriov_get_totalvfs(dev);
if (numvfs > 0) {
err = pci_enable_sriov(dev, numvfs);
if (unlikely(err)) {
mods_error_printk(
"failed to enable sriov on dev %04x:%02x:%02x.%x %s numvfs=%d (totalvfs=%d), err=%d\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dev->is_physfn ? "physfn" : "virtfn",
numvfs,
totalvfs,
err);
numvfs = err;
} else {
mods_info_printk(
"enabled sriov on dev %04x:%02x:%02x.%x %s numvfs=%d (totalvfs=%d)\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dev->is_physfn ? "physfn" : "virtfn",
numvfs,
totalvfs);
}
} else {
pci_disable_sriov(dev);
numvfs = 0;
mods_info_printk(
"disabled sriov on dev %04x:%02x:%02x.%x %s (totalvfs=%d)\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dev->is_physfn ? "physfn" : "virtfn",
totalvfs);
}
/* If this function has been invoked via an ioctl, remember numvfs */
if (!err) {
struct en_dev_entry *dpriv = pci_get_drvdata(dev);
if (dpriv)
dpriv->num_vfs = numvfs;
}
LOG_EXT();
return numvfs;
}
static int esc_mods_set_num_vf(struct mods_client *client,
struct MODS_SET_NUM_VF *p)
{
int err;
struct pci_dev *dev = NULL;
struct en_dev_entry *dpriv;
LOG_ENT();
if (p->numvfs > 0xFFFFU) {
cl_error("invalid input numfs %u\n", p->numvfs);
err = -EINVAL;
goto error;
}
/* Get the PCI device structure for the specified device from kernel */
err = mods_find_pci_dev(client, &p->dev, &dev);
if (unlikely(err)) {
if (err == -ENODEV)
cl_error("dev %04x:%02x:%02x.%x not found\n",
p->dev.domain,
p->dev.bus,
p->dev.device,
p->dev.function);
goto error;
}
dpriv = pci_get_drvdata(dev);
if (!dpriv || !is_valid_client_id(dpriv->client_id)) {
cl_error(
"failed to enable sriov, dev %04x:%02x:%02x.%x was not enabled\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn));
err = -EINVAL;
goto error;
}
if (dpriv->client_id != client->client_id) {
cl_error(
"invalid client for dev %04x:%02x:%02x.%x, expected %u\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dpriv->client_id);
err = -EBUSY;
goto error;
}
err = mods_pci_sriov_configure(dev, (u16)p->numvfs);
error:
pci_dev_put(dev);
LOG_EXT();
return err;
}
static int esc_mods_set_total_vf(struct mods_client *client,
struct MODS_SET_NUM_VF *p)
{
int err;
struct pci_dev *dev = NULL;
struct en_dev_entry *dpriv;
LOG_ENT();
if (p->numvfs > 0xFFFFU) {
cl_error("invalid input numfs %u\n", p->numvfs);
err = -EINVAL;
goto error;
}
/* Get the PCI device structure for the specified device from kernel */
err = mods_find_pci_dev(client, &p->dev, &dev);
if (unlikely(err)) {
if (err == -ENODEV)
cl_error("dev %04x:%02x:%02x.%x not found\n",
p->dev.domain,
p->dev.bus,
p->dev.device,
p->dev.function);
goto error;
}
dpriv = pci_get_drvdata(dev);
if (!dpriv || !is_valid_client_id(dpriv->client_id)) {
cl_error(
"failed to enable sriov, dev %04x:%02x:%02x.%x was not enabled\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn));
err = -EBUSY;
goto error;
}
if (dpriv->client_id != client->client_id) {
cl_error(
"invalid client for dev %04x:%02x:%02x.%x, expected %u\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn),
dpriv->client_id);
err = -EBUSY;
goto error;
}
err = pci_sriov_set_totalvfs(dev, (u16)p->numvfs);
if (unlikely(err)) {
cl_error(
"failed to set totalvfs=%d on dev %04x:%02x:%02x.%x, err=%d\n",
p->numvfs,
p->dev.domain,
p->dev.bus,
p->dev.device,
p->dev.function,
err);
} else
cl_info("set totalvfs %d on dev %04x:%02x:%02x.%x\n",
p->numvfs,
p->dev.domain,
p->dev.bus,
p->dev.device,
p->dev.function);
error:
pci_dev_put(dev);
LOG_EXT();
return err;
}
#endif
void mods_set_debug_level(int mask)
{
debug = mask;
}
int mods_get_debug_level(void)
{
return debug;
}
int mods_check_debug_level(int mask)
{
return ((debug & mask) == mask) ? 1 : 0;
}
void mods_set_multi_instance(int mi)
{
multi_instance = (mi > 0) ? 1 : -1;
}
int mods_get_multi_instance(void)
{
return multi_instance > 0;
}
void mods_set_force_qual_mode(int qm)
{
force_qual_mode = qm;
}
int mods_get_force_qual_mode(void)
{
return force_qual_mode;
}
/*********************
* CLIENT MANAGEMENT *
*********************/
static struct mods_priv mp;
static struct mods_client *alloc_client(void)
{
unsigned int idx;
unsigned int max_clients = 1;
LOG_ENT();
if (mods_get_multi_instance() ||
(mods_get_access_token() != MODS_ACCESS_TOKEN_NONE))
max_clients = MODS_MAX_CLIENTS;
for (idx = 1; idx <= max_clients; idx++) {
if (!test_and_set_bit(idx - 1U, mp.client_flags)) {
struct mods_client *client = mods_client_from_id(idx);
memset(client, 0, sizeof(*client));
client->client_id = idx;
client->access_token = MODS_ACCESS_TOKEN_NONE;
atomic_set(&client->last_bad_dbdf, -1);
cl_debug(DEBUG_IOCTL,
"open client %u\n",
idx);
mutex_init(&client->mtx);
spin_lock_init(&client->irq_lock);
init_waitqueue_head(&client->interrupt_event);
INIT_LIST_HEAD(&client->irq_list);
INIT_LIST_HEAD(&client->mem_alloc_list);
INIT_LIST_HEAD(&client->enabled_devices);
INIT_LIST_HEAD(&client->mem_map_list);
INIT_LIST_HEAD(&client->free_mem_list);
LOG_EXT();
return client;
}
}
LOG_EXT();
return NULL;
}
static void free_client(u8 client_id)
{
struct mods_client *client = mods_client_from_id(client_id);
LOG_ENT();
memset(client, 0, sizeof(*client));
/* Indicate the client_id is free */
clear_bit((unsigned int)client_id - 1U, mp.client_flags);
cl_debug(DEBUG_IOCTL, "closed client\n");
LOG_EXT();
}
struct mods_client *mods_client_from_id(u8 client_id)
{
return &mp.clients[client_id - 1];
}
int mods_is_client_enabled(u8 client_id)
{
return test_bit(client_id - 1, mp.client_flags);
}
u32 mods_get_access_token(void)
{
return access_token;
}
static int validate_client(struct mods_client *client)
{
if (!client) {
mods_error_printk("invalid client\n");
return false;
}
if (client->client_id < 1 ||
client->client_id > MODS_MAX_CLIENTS ||
!mods_is_client_enabled(client->client_id)) {
cl_error("invalid client id\n");
return false;
}
return true;
}
static int mods_set_access_token(u32 tok)
{
/* When setting a null token, the existing token must match the
* provided token, when setting a non-null token the existing token
* must be null, use atomic compare/exchange to set it
*/
u32 req_old_token =
(tok == MODS_ACCESS_TOKEN_NONE) ?
access_token : MODS_ACCESS_TOKEN_NONE;
if (cmpxchg(&access_token, req_old_token, tok) != req_old_token)
return -EFAULT;
return OK;
}
static int mods_check_access_token(struct mods_client *client)
{
if (client->access_token != mods_get_access_token()) {
cl_error("invalid access token %u\n", client->access_token);
return -EFAULT;
}
return OK;
}
/******************************
* INIT/EXIT MODULE FUNCTIONS *
******************************/
static int __init mods_init_module(void)
{
int rc;
LOG_ENT();
memset(&mp, 0, sizeof(mp));
mods_init_irq();
rc = misc_register(&mods_dev);
if (rc < 0)
return -EBUSY;
#if defined(CONFIG_PCI)
rc = pci_register_driver(&mods_pci_driver);
if (rc < 0)
return -EBUSY;
#endif
#if defined(MODS_HAS_TEGRA) && defined(CONFIG_COMMON_CLK)
mods_init_clock_api();
#endif
rc = mods_create_debugfs(&mods_dev);
if (rc < 0)
return rc;
rc = mods_init_dmabuf();
if (rc < 0)
return rc;
#if defined(MODS_HAS_TEGRA)
rc = smmu_driver_init();
if (rc < 0)
return rc;
#if defined(CONFIG_DMA_ENGINE)
rc = mods_init_dma();
if (rc < 0)
return rc;
#endif
#endif
#if defined(MODS_HAS_ARM_FFA)
rc = mods_ffa_abi_register();
if (rc < 0)
mods_warning_printk("error on mods_ffa_abi_register returned %d\n", rc);
#endif
mods_info_printk("*** WARNING: DIAGNOSTIC DRIVER LOADED ***\n");
mods_info_printk("driver loaded, version %x.%02x\n",
(MODS_DRIVER_VERSION >> 8),
(MODS_DRIVER_VERSION & 0xFF));
if (debug)
mods_info_printk("debug level 0x%x\n", debug);
LOG_EXT();
return OK;
}
static void __exit mods_exit_module(void)
{
int i;
LOG_ENT();
mods_exit_dmabuf();
mods_remove_debugfs();
for (i = 0; i < MODS_MAX_CLIENTS; i++) {
if (test_bit(i, mp.client_flags))
free_client(i + 1);
}
#if defined(MODS_HAS_TEGRA)
#if defined(CONFIG_DMA_ENGINE)
mods_exit_dma();
#endif
smmu_driver_exit();
#endif
#if defined(CONFIG_PCI)
pci_unregister_driver(&mods_pci_driver);
#endif
misc_deregister(&mods_dev);
#if defined(MODS_HAS_TEGRA) && defined(CONFIG_COMMON_CLK)
mods_shutdown_clock_api();
#endif
#if defined(MODS_HAS_ARM_FFA)
mods_ffa_abi_unregister();
#endif
mods_free_mem_reservations();
mods_info_printk("driver unloaded\n");
LOG_EXT();
}
/***************************
* KERNEL INTERFACE SET UP *
***************************/
module_init(mods_init_module);
module_exit(mods_exit_module);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("NVIDIA diagnostic driver");
#define STRING_VALUE(x) #x
#define MAKE_MODULE_VERSION(x, y) STRING_VALUE(x) "." STRING_VALUE(y)
MODULE_VERSION(MAKE_MODULE_VERSION(MODS_DRIVER_VERSION_MAJOR,
MODS_DRIVER_VERSION_MINOR));
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug,
"debug bitflags (2=ioctl 4=pci 8=acpi 16=irq 32=mem 64=fun +256=detailed)");
module_param(multi_instance, int, 0644);
MODULE_PARM_DESC(multi_instance,
"allows more than one client to simultaneously open the driver");
#if defined(MODS_HAS_ARM_SMCCC)
module_param(force_qual_mode, int, 0644);
MODULE_PARM_DESC(force_qual_mode, "enable qual mode during driver probing");
#endif
/********************
* HELPER FUNCTIONS *
********************/
static void mods_disable_all_devices(struct mods_client *client)
{
#ifdef CONFIG_PCI
struct list_head *head = &client->enabled_devices;
struct en_dev_entry *entry;
struct en_dev_entry *tmp;
#ifdef MODS_HAS_SRIOV
mutex_lock(mods_get_irq_mutex());
list_for_each_entry_safe(entry, tmp, head, list) {
struct en_dev_entry *dpriv = pci_get_drvdata(entry->dev);
if (dpriv->num_vfs == 0) {
mods_disable_device(client, entry->dev);
list_del(&entry->list);
}
}
mutex_unlock(mods_get_irq_mutex());
list_for_each_entry(entry, head, list) {
pci_disable_sriov(entry->dev);
}
#endif
mutex_lock(mods_get_irq_mutex());
list_for_each_entry_safe(entry, tmp, head, list) {
mods_disable_device(client, entry->dev);
list_del(&entry->list);
}
mutex_unlock(mods_get_irq_mutex());
if (client->cached_dev) {
pci_dev_put(client->cached_dev);
client->cached_dev = NULL;
}
#else
WARN_ON(!list_empty(&client->enabled_devices));
#endif
}
#if defined(MODS_HAS_CONSOLE_LOCK)
static int mods_resume_console(struct mods_client *client);
#else
static inline int mods_resume_console(struct mods_client *client) { return 0; }
#endif
/*********************
* MAPPING FUNCTIONS *
*********************/
static int register_mapping(struct mods_client *client,
struct MODS_MEM_INFO *p_mem_info,
phys_addr_t phys_addr,
struct SYS_MAP_MEMORY *p_map_mem,
unsigned long virtual_address,
unsigned long mapping_offs,
unsigned long mapping_length)
{
LOG_ENT();
p_map_mem->phys_addr = phys_addr;
p_map_mem->virtual_addr = virtual_address;
p_map_mem->mapping_offs = mapping_offs;
p_map_mem->mapping_length = mapping_length;
p_map_mem->p_mem_info = p_mem_info;
list_add(&p_map_mem->list, &client->mem_map_list);
cl_debug(DEBUG_MEM_DETAILED,
"map alloc %p as %p: phys 0x%llx, virt 0x%lx, size 0x%lx\n",
p_mem_info,
p_map_mem,
(unsigned long long)phys_addr,
virtual_address,
mapping_length);
LOG_EXT();
return OK;
}
static pgprot_t get_prot(struct mods_client *client,
u8 mem_type,
pgprot_t prot)
{
switch (mem_type) {
case MODS_ALLOC_CACHED:
return prot;
case MODS_ALLOC_UNCACHED:
return MODS_PGPROT_UC(prot);
case MODS_ALLOC_WRITECOMBINE:
return MODS_PGPROT_WC(prot);
default:
cl_warn("unsupported memory type: %u\n", mem_type);
return prot;
}
}
static int get_prot_for_range(struct mods_client *client,
phys_addr_t phys_addr,
unsigned long size,
pgprot_t *prot)
{
const phys_addr_t req_end = phys_addr + size;
const phys_addr_t range_begin = client->mem_type.phys_addr;
const phys_addr_t range_end = range_begin + client->mem_type.size;
/* Check overlap with set memory type range */
if (phys_addr < range_end && req_end > range_begin) {
/* Check if requested range is completely inside */
if (likely(phys_addr >= range_begin && req_end <= range_end)) {
*prot = get_prot(client, client->mem_type.type, *prot);
return 0;
}
cl_error("mmap range [0x%llx, 0x%llx] does not match set memory type range [0x%llx, 0x%llx]\n",
(unsigned long long)phys_addr,
(unsigned long long)req_end,
(unsigned long long)range_begin,
(unsigned long long)range_end);
return -EINVAL;
}
return 0;
}
const char *mods_get_prot_str(u8 mem_type)
{
switch (mem_type) {
case MODS_ALLOC_CACHED:
return "WB";
case MODS_ALLOC_UNCACHED:
return "UC";
case MODS_ALLOC_WRITECOMBINE:
return "WC";
default:
return "unknown";
}
}
static const char *get_prot_str_for_range(struct mods_client *client,
phys_addr_t phys_addr,
unsigned long size)
{
const phys_addr_t req_end = phys_addr + size;
const phys_addr_t range_begin = client->mem_type.phys_addr;
const phys_addr_t range_end = range_begin + client->mem_type.size;
/* Check overlap with set memory type range */
if (phys_addr < range_end && req_end > range_begin)
return mods_get_prot_str(client->mem_type.type);
return "default";
}
/********************
* PCI ERROR FUNCTIONS *
********************/
#if defined(CONFIG_PCI)
static pci_ers_result_t mods_pci_error_detected(struct pci_dev *dev,
pci_channel_state_t state)
{
mods_debug_printk(DEBUG_PCI,
"pci_error_detected dev %04x:%02x:%02x.%x\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn));
return PCI_ERS_RESULT_CAN_RECOVER;
}
static pci_ers_result_t mods_pci_mmio_enabled(struct pci_dev *dev)
{
mods_debug_printk(DEBUG_PCI,
"pci_mmio_enabled dev %04x:%02x:%02x.%x\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn));
return PCI_ERS_RESULT_NEED_RESET;
}
static void mods_pci_resume(struct pci_dev *dev)
{
mods_debug_printk(DEBUG_PCI,
"pci_resume dev %04x:%02x:%02x.%x\n",
pci_domain_nr(dev->bus),
dev->bus->number,
PCI_SLOT(dev->devfn),
PCI_FUNC(dev->devfn));
}
#endif
/********************
* KERNEL FUNCTIONS *
********************/
static void mods_krnl_vma_open(struct vm_area_struct *vma)
{
struct SYS_MAP_MEMORY *p_map_mem;
LOG_ENT();
mods_debug_printk(DEBUG_MEM_DETAILED,
"open vma, virt 0x%lx, size 0x%lx, phys 0x%llx\n",
vma->vm_start,
vma->vm_end - vma->vm_start,
(unsigned long long)vma->vm_pgoff << PAGE_SHIFT);
p_map_mem = vma->vm_private_data;
if (p_map_mem)
atomic_inc(&p_map_mem->usage_count);
LOG_EXT();
}
static void mods_krnl_vma_close(struct vm_area_struct *vma)
{
struct SYS_MAP_MEMORY *p_map_mem;
LOG_ENT();
p_map_mem = vma->vm_private_data;
if (p_map_mem && atomic_dec_and_test(&p_map_mem->usage_count)) {
struct mods_client *client = p_map_mem->client;
if (p_map_mem->mapping_length) {
mutex_lock(&client->mtx);
list_del(&p_map_mem->list);
mutex_unlock(&client->mtx);
}
mods_debug_printk(DEBUG_MEM_DETAILED,
"closed vma, virt 0x%lx\n",
vma->vm_start);
vma->vm_private_data = NULL;
kfree(p_map_mem);
atomic_dec(&client->num_allocs);
}
LOG_EXT();
}
#ifdef CONFIG_HAVE_IOREMAP_PROT
static int mods_krnl_vma_access(struct vm_area_struct *vma,
unsigned long addr,
void *buf,
int len,
int write)
{
struct SYS_MAP_MEMORY *p_map_mem = vma->vm_private_data;
struct mods_client *client;
unsigned long map_offs;
int err = OK;
LOG_ENT();
if (!p_map_mem) {
LOG_EXT();
return -EINVAL;
}
client = p_map_mem->client;
cl_debug(DEBUG_MEM_DETAILED,
"access vma [virt 0x%lx, size 0x%lx, phys 0x%llx] at virt 0x%lx, len 0x%x\n",
vma->vm_start,
vma->vm_end - vma->vm_start,
(unsigned long long)vma->vm_pgoff << PAGE_SHIFT,
addr,
len);
if (unlikely(mutex_lock_interruptible(&client->mtx))) {
LOG_EXT();
return -EINTR;