/
kern_bus_gp100.c
1194 lines (1067 loc) · 41.9 KB
/
kern_bus_gp100.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
/*
* SPDX-FileCopyrightText: Copyright (c) 2014-2023 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 "core/core.h"
#include "gpu/gpu.h"
#include "mem_mgr/vaspace.h"
#include "gpu/bus/kern_bus.h"
#include "gpu/bus/p2p_api.h"
#include "gpu/mem_mgr/mem_mgr.h"
#include "gpu/mem_mgr/fermi_dma.h"
#include "kernel/gpu/nvlink/kernel_nvlink.h"
#include "vgpu/vgpu_events.h"
#include "platform/sli/sli.h"
#include "published/pascal/gp100/dev_ram.h"
/*!
* @brief Create a P2P mapping to a given peer GPU
*
* @param[in] pGpu0 (local GPU)
* @param[in] pKernelBus0 (local GPU)
* @param[in] pGpu1 (remote GPU)
* @param[in] pKernelBus1 (remote GPU)
* @param[out] peer0 Peer ID (local to remote)
* @param[out] peer1 Peer ID (remote to local)
* @param[in] attributes Sepcial attributes for the mapping
*
* return NV_OK on success
*/
NV_STATUS
kbusCreateP2PMapping_GP100
(
OBJGPU *pGpu0,
KernelBus *pKernelBus0,
OBJGPU *pGpu1,
KernelBus *pKernelBus1,
NvU32 *peer0,
NvU32 *peer1,
NvU32 attributes
)
{
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, _NVLINK, attributes) ||
FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, _NVLINK_INDIRECT, attributes))
{
return kbusCreateP2PMappingForNvlink_HAL(pGpu0, pKernelBus0, pGpu1, pKernelBus1, peer0, peer1, attributes);
}
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, _PCIE, attributes))
{
return kbusCreateP2PMappingForMailbox_HAL(pGpu0, pKernelBus0, pGpu1, pKernelBus1, peer0, peer1, attributes);
}
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, _PCIE_BAR1, attributes))
{
return kbusCreateP2PMappingForBar1P2P_GH100(pGpu0, pKernelBus0, pGpu1, pKernelBus1, attributes);
}
NV_PRINTF(LEVEL_ERROR, "P2P type %d is not supported\n", DRF_VAL(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, attributes));
return NV_ERR_NOT_SUPPORTED;
}
// Grab GPU locks before RPCing into GSP-RM for NVLink RPCs
static NV_STATUS
_kbusExecGspRmRpcForNvlink
(
OBJGPU *pGpu,
NvU32 cmd,
void *paramAddr,
NvU32 paramSize
)
{
NvU32 gpuMaskRelease = 0;
NvU32 gpuMaskInitial = rmGpuLocksGetOwnedMask();
NvU32 gpuMask = gpuMaskInitial | NVBIT(pGpu->gpuInstance);
NV_STATUS status = NV_OK;
//
// XXX Bug 1795328: Fix P2P path to acquire locks for the GPU
// Due to platform differences in the P2P path, the GPU lock is not
// consistently held at this point in the call stack. This function
// requires exclusive access to RM/PMU data structures to update HSHUB,
// and therefore requires the GPU lock to be held at this point.
// This check should be removed once the P2P paths have been updated to
// acquire the GPU locks consistently for all platforms.
//
if (IS_GSP_CLIENT(pGpu))
{
if (!rmGpuGroupLockIsOwner(pGpu->gpuInstance, GPU_LOCK_GRP_MASK, &gpuMask))
{
status = rmGpuGroupLockAcquire(pGpu->gpuInstance,
GPU_LOCK_GRP_MASK,
GPU_LOCK_FLAGS_SAFE_LOCK_UPGRADE,
RM_LOCK_MODULES_NVLINK,
&gpuMask);
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR, "Failed to acquire locks for gpumask 0x%x\n", gpuMask);
return status;
}
gpuMaskRelease = (gpuMask & (~gpuMaskInitial));
}
}
RM_API *pRmApi = GPU_GET_PHYSICAL_RMAPI(pGpu);
status = pRmApi->Control(pRmApi,
pGpu->hInternalClient,
pGpu->hInternalSubdevice,
cmd, paramAddr, paramSize);
if (gpuMaskRelease)
{
rmGpuGroupLockRelease(gpuMaskRelease, GPUS_LOCK_FLAGS_NONE);
}
return status;
}
/*!
* @brief Create NVLink mapping to a given peer GPU
*
* @param[in] pGpu0 (Local)
* @param[in] pKernelBus0 (Local)
* @param[in] pGpu1 (Remote)
* @param[in] peerId peerID
* @param[in] attributes P2PApi attributes
*
* return NV_OK on success
*/
static NV_STATUS
_kbusCreateNvlinkPeerMapping
(
OBJGPU *pGpu0,
KernelBus *pKernelBus0,
OBJGPU *pGpu1,
NvU32 peerId,
NvU32 attributes
)
{
NV2080_CTRL_BUS_SET_P2P_MAPPING_PARAMS params;
NV_STATUS status = NV_OK;
OBJSYS *pSys = SYS_GET_INSTANCE();
NV_ASSERT_OK_OR_RETURN(osAcquireRmSema(pSys->pSema));
portMemSet(¶ms, 0, sizeof(params));
params.connectionType = NV2080_CTRL_CMD_BUS_SET_P2P_MAPPING_CONNECTION_TYPE_NVLINK;
params.peerId = peerId;
params.bEgmPeer = FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _REMOTE_EGM, _YES, attributes);
params.bUseUuid = NV_FALSE;
params.remoteGpuId = pGpu1->gpuId;
params.bSpaAccessOnly = FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _LINK_TYPE, _SPA, attributes);
_kbusExecGspRmRpcForNvlink(pGpu0, NV2080_CTRL_CMD_BUS_SET_P2P_MAPPING,
¶ms, sizeof(params));
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%d NV2080_CTRL_CMD_BUS_SET_P2P_MAPPING failed for peer%d\n",
gpuGetInstance(pGpu0), peerId);
}
osReleaseRmSema(pSys->pSema, NULL);
return status;
}
/*!
* @brief Create a P2P mapping to a given peer GPU
*
* @param[in] pGpu0 (local GPU)
* @param[in] pKernelBus0 (local GPU)
* @param[in] pGpu1 (remote GPU)
* @param[in] pKernelBus1 (remote GPU)
* @param[out] peer0 Peer ID (local to remote)
* @param[out] peer1 Peer ID (remote to local)
* @param[in] attributes Sepcial attributes for the mapping
*
* return NV_OK on success
*/
NV_STATUS
kbusCreateP2PMappingForNvlink_GP100
(
OBJGPU *pGpu0,
KernelBus *pKernelBus0,
OBJGPU *pGpu1,
KernelBus *pKernelBus1,
NvU32 *peer0,
NvU32 *peer1,
NvU32 attributes
)
{
NvU32 gpu0Instance = gpuGetInstance(pGpu0);
NvU32 gpu1Instance = gpuGetInstance(pGpu1);
NvBool bLoopback = (pGpu0 == pGpu1);
NV_STATUS status = NV_OK;
NvBool bEgmPeer = FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _REMOTE_EGM, _YES, attributes);
if (peer0 == NULL || peer1 == NULL)
{
return NV_ERR_INVALID_ARGUMENT;
}
// Set the default RM mapping if peer id's are not explicitly provided
if (*peer0 == BUS_INVALID_PEER || *peer1 == BUS_INVALID_PEER)
{
if (bLoopback && !bEgmPeer)
{
if (pKernelBus0->p2pMapSpecifyId)
{
*peer0 = *peer1 = pKernelBus0->p2pMapPeerId;
}
else
{
// If no static mapping is found, set peer id as 0 for loopback
*peer0 = *peer1 = 0;
NV_CHECK_OK_OR_RETURN(LEVEL_ERROR,
kbusReserveP2PPeerIds_HAL(pGpu0, pKernelBus0, NVBIT(0)));
}
}
else
{
// Get the peer ID pGpu0 should use for P2P over NVLINK to pGpu1
if ((status = kbusGetNvlinkP2PPeerId_HAL(pGpu0, pKernelBus0,
pGpu1, pKernelBus1,
peer0, attributes)) != NV_OK)
{
return status;
}
// EGM loopback
if (bLoopback)
{
// The loopback check here becomes true only in the EGM case
NV_ASSERT_OR_RETURN(bEgmPeer, NV_ERR_INVALID_STATE);
*peer1 = *peer0;
}
else
// Get the peer ID pGpu1 should use for P2P over NVLINK to pGpu0
if ((status = kbusGetNvlinkP2PPeerId_HAL(pGpu1, pKernelBus1,
pGpu0, pKernelBus0,
peer1, attributes)) != NV_OK)
{
return status;
}
if (*peer0 == BUS_INVALID_PEER || *peer1 == BUS_INVALID_PEER)
{
return NV_ERR_INVALID_REQUEST;
}
}
NV_PRINTF(LEVEL_INFO, "- P2P: Using Default RM mapping for P2P.\n");
}
if (bEgmPeer)
{
NV_PRINTF(LEVEL_INFO, "EGM peer\n");
}
//
// Does the mapping already exist between the given pair of GPUs using the peerIDs
// *peer0 and *peer1 respectively ?
//
if ((pKernelBus0->p2p.busNvlinkPeerNumberMask[gpu1Instance] & NVBIT(*peer0)) &&
(pKernelBus1->p2p.busNvlinkPeerNumberMask[gpu0Instance] & NVBIT(*peer1)))
{
//
// Increment the mapping refcount per peerID - since there is another usage
// of a mapping that is using this peerID
//
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[*peer0]++;
pKernelBus1->p2p.busNvlinkMappingRefcountPerPeerId[*peer1]++;
//
// Increment the mapping refcount per GPU - since there is another usage of
// the mapping to the given remote GPU
//
pKernelBus0->p2p.busNvlinkMappingRefcountPerGpu[gpu1Instance]++;
pKernelBus1->p2p.busNvlinkMappingRefcountPerGpu[gpu0Instance]++;
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _LINK_TYPE, _SPA, attributes))
{
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[*peer0]++;
pKernelBus1->p2p.busNvlinkMappingRefcountPerPeerId[*peer1]++;
}
NV_PRINTF(LEVEL_INFO,
"- P2P: Peer mapping is already in use for gpu instances %x and %x "
"with peer id's %d and %d. Increasing the mapping refcounts for the"
" peer IDs to %d and %d respectively.\n",
gpu0Instance, gpu1Instance, *peer0, *peer1,
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[*peer0],
pKernelBus1->p2p.busNvlinkMappingRefcountPerPeerId[*peer1]);
return NV_OK;
}
//
// Reached here implies the mapping between the given pair of GPUs using the peerIDs
// *peer0 and *peer1 does not exist. Create the mapping
//
// If we're in loopback mode check for specified peer ID arg from RM or MODS
if (!bEgmPeer && bLoopback && pKernelBus0->p2pMapSpecifyId)
{
if ((pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[pKernelBus0->p2pMapPeerId] == 0) &&
(pKernelBus1->p2p.busNvlinkMappingRefcountPerPeerId[pKernelBus1->p2pMapPeerId] == 0))
{
*peer0 = *peer1 = pKernelBus0->p2pMapPeerId;
}
else
{
NV_PRINTF(LEVEL_ERROR,
"- ERROR: Peer ID %d is already in use. Default RM P2P mapping "
"will be used for loopback connection.\n",
pKernelBus0->p2pMapPeerId);
}
}
// Set the peer IDs in the corresponding peer number masks
pKernelBus0->p2p.busNvlinkPeerNumberMask[gpu1Instance] |= NVBIT(*peer0);
pKernelBus1->p2p.busNvlinkPeerNumberMask[gpu0Instance] |= NVBIT(*peer1);
//
// Increment the mapping refcount per peerID - since there is a new mapping that
// will use this peerID
//
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[*peer0]++;
pKernelBus1->p2p.busNvlinkMappingRefcountPerPeerId[*peer1]++;
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _LINK_TYPE, _SPA, attributes))
{
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[*peer0]++;
pKernelBus1->p2p.busNvlinkMappingRefcountPerPeerId[*peer1]++;
}
//
// Increment the mapping refcount per GPU - since there a new mapping now to the
// given remote GPU
//
pKernelBus0->p2p.busNvlinkMappingRefcountPerGpu[gpu1Instance]++;
pKernelBus1->p2p.busNvlinkMappingRefcountPerGpu[gpu0Instance]++;
NV_PRINTF(LEVEL_INFO,
"added NVLink P2P mapping between GPU%u (peer %u) and GPU%u (peer %u)\n",
gpu0Instance, *peer0, gpu1Instance, *peer1);
if (IS_VGPU_GSP_PLUGIN_OFFLOAD_ENABLED(pGpu0))
{
NV_ASSERT_OK_OR_RETURN(_kbusCreateNvlinkPeerMapping(pGpu0, pKernelBus0, pGpu1, *peer0, attributes));
NV_ASSERT_OK_OR_RETURN(_kbusCreateNvlinkPeerMapping(pGpu1, pKernelBus1, pGpu0, *peer1, attributes));
return status;
}
else
{
KernelNvlink *pKernelNvlink0 = GPU_GET_KERNEL_NVLINK(pGpu0);
KernelNvlink *pKernelNvlink1 = GPU_GET_KERNEL_NVLINK(pGpu1);
NV2080_CTRL_NVLINK_ENABLE_NVLINK_PEER_PARAMS params;
if (pKernelNvlink0 == NULL || pKernelNvlink1 == NULL)
{
return NV_ERR_INVALID_REQUEST;
}
portMemSet(¶ms, 0, sizeof(params));
params.peerMask = NVBIT(*peer0);
params.bEnable = NV_TRUE;
// Set the NVLink USE_NVLINK_PEER fields in the LTCS registers for GPU0
status = knvlinkExecGspRmRpc(pGpu0, pKernelNvlink0,
NV2080_CTRL_CMD_NVLINK_ENABLE_NVLINK_PEER,
(void *)¶ms, sizeof(params));
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%d Failed to ENABLE USE_NVLINK_PEER for peer%d\n",
gpuGetInstance(pGpu0), *peer0);
return status;
}
portMemSet(¶ms, 0, sizeof(params));
params.peerMask = NVBIT(*peer1);
params.bEnable = NV_TRUE;
// Set the NVLink USE_NVLINK_PEER fields in the LTCS registers for GPU1
status = knvlinkExecGspRmRpc(pGpu1, pKernelNvlink1,
NV2080_CTRL_CMD_NVLINK_ENABLE_NVLINK_PEER,
(void *)¶ms, sizeof(params));
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%d Failed to ENABLE USE_NVLINK_PEER for peer%d\n",
gpuGetInstance(pGpu1), *peer1);
return status;
}
// Enable the peer configuration in the HSHUB config registers
knvlinkSetupPeerMapping_HAL(pGpu0, pKernelNvlink0, pGpu1, *peer0);
knvlinkSetupPeerMapping_HAL(pGpu1, pKernelNvlink1, pGpu0, *peer1);
return NV_OK;
}
}
/*!
* @brief Remove NVLink mapping to a given peer GPU
*
* @param[in] pGpu0 (Local)
* @param[in] pKernelBus0 (Local)
* @param[in] pGpu1 (Remote)
* @param[in] peerId peerID
*
* return NV_OK on success
*/
static NV_STATUS
_kbusRemoveNvlinkPeerMapping
(
OBJGPU *pGpu0,
KernelBus *pKernelBus0,
OBJGPU *pGpu1,
NvU32 peerId,
NvU32 attributes
)
{
NV_STATUS status = NV_OK;
NvU32 peerGpuInst = gpuGetInstance(pGpu1);
NvBool bBufferReady = NV_FALSE;
// If no peer mapping exists between the GPUs, return NV_WARN_NOTHING_TO_DO
if ((pKernelBus0->p2p.busNvlinkPeerNumberMask[peerGpuInst] & NVBIT(peerId)) == 0)
{
return NV_WARN_NOTHING_TO_DO;
}
// A programming error somewhere in RM: mapping exists with a zero refcount
if ((pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[peerId] == 0) ||
(pKernelBus0->p2p.busNvlinkMappingRefcountPerGpu[peerGpuInst] == 0))
{
DBG_BREAKPOINT();
return NV_ERR_INVALID_STATE;
}
// Decrement the mapping refcount associated with the peerID
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[peerId]--;
// Decrement the mapping refcount for the given remote GPU1
pKernelBus0->p2p.busNvlinkMappingRefcountPerGpu[peerGpuInst]--;
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _LINK_TYPE, _SPA, attributes))
{
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[peerId]--;
}
//
// If mapping refcount to remote GPU1 is 0, this implies the peerID is no
// longer used for P2P from GPU0 to GPU1. Update busNvlinkPeerNumberMask
//
// Special case:
// Peers connected through NvSwitch in which case all the peers use
// peer id 0 and the refcount for peer id 0 wouldn't reach 0 until
// P2P between all the peers is destroyed.
// busNvlinkMappingRefcountPerGpu == 0 check is done in this case to remove
// the peer id from busNvlinkPeerNumberMask[peerGpuInst]
// Two peer ids are used to reach the same GPU, one for HBM and one for
// EGM. In that case busNvlinkMappingRefcountPerGpu isn't going to
// reach 0 until both the peer ids are removed. In this case,
// busNvlinkMappingRefcountPerPeerId[peerId] == 0 check is required to
// remove the peer id from busNvlinkPeerNumberMask[peerGpuInst].
//
if (pKernelBus0->p2p.busNvlinkMappingRefcountPerGpu[peerGpuInst] == 0 ||
pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[peerId] == 0)
{
NV_PRINTF(LEVEL_INFO,
"Removing mapping for GPU%u peer %u (GPU%u)\n",
gpuGetInstance(pGpu0), peerId, peerGpuInst);
pKernelBus0->p2p.busNvlinkPeerNumberMask[peerGpuInst] &= ~NVBIT(peerId);
}
//
// Can the peerID be freed? The peer ID can *only* be freed if it is not being
// used for P2P to any GPU. Check the mapping refcount for the given peerID
//
if (pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[peerId] == 0)
{
NV2080_CTRL_BUS_UNSET_P2P_MAPPING_PARAMS params;
OBJSYS *pSys = SYS_GET_INSTANCE();
NV_ASSERT(pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[peerId] == 0);
NV_PRINTF(LEVEL_INFO,
"PeerID %u is not being used for P2P from GPU%d to any other "
"remote GPU. Can be freed\n",
peerId, gpuGetInstance(pGpu0));
if (pKernelBus0->getProperty(pKernelBus0,
PDB_PROP_KBUS_NVLINK_DECONFIG_HSHUB_ON_NO_MAPPING))
{
// Before removing the NVLink peer mapping in HSHUB flush both ends
kbusFlush_HAL(pGpu0, pKernelBus0, BUS_FLUSH_VIDEO_MEMORY);
kbusFlush_HAL(pGpu1, GPU_GET_KERNEL_BUS(pGpu1), BUS_FLUSH_VIDEO_MEMORY);
}
if (IS_VGPU_GSP_PLUGIN_OFFLOAD_ENABLED(pGpu0))
{
NV_ASSERT_OK_OR_RETURN(osAcquireRmSema(pSys->pSema));
portMemSet(¶ms, 0, sizeof(params));
params.connectionType = NV2080_CTRL_CMD_BUS_SET_P2P_MAPPING_CONNECTION_TYPE_NVLINK;
params.peerId = peerId;
params.bUseUuid = NV_FALSE;
params.remoteGpuId = pGpu1->gpuId;
_kbusExecGspRmRpcForNvlink(pGpu0, NV2080_CTRL_CMD_BUS_UNSET_P2P_MAPPING,
¶ms, sizeof(params));
osReleaseRmSema(pSys->pSema, NULL);
}
else
{
KernelNvlink *pKernelNvlink0 = GPU_GET_KERNEL_NVLINK(pGpu0);
NV2080_CTRL_NVLINK_ENABLE_NVLINK_PEER_PARAMS params;
NV_ASSERT_OR_RETURN(pKernelNvlink0 != NULL, NV_ERR_NOT_SUPPORTED);
portMemSet(¶ms, 0, sizeof(params));
params.peerMask = NVBIT(peerId);
params.bEnable = NV_FALSE;
// Unset the NVLink USE_NVLINK_PEER fields in the LTCS registers for GPU0
status = knvlinkExecGspRmRpc(pGpu0, pKernelNvlink0,
NV2080_CTRL_CMD_NVLINK_ENABLE_NVLINK_PEER,
(void *)¶ms, sizeof(params));
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%d Failed to UNSET USE_NVLINK_PEER for peer%d\n",
gpuGetInstance(pGpu0), peerId);
return status;
}
// Disable the peer configuration in the HSHUB config registers
if ((pKernelBus0->getProperty(pKernelBus0,
PDB_PROP_KBUS_NVLINK_DECONFIG_HSHUB_ON_NO_MAPPING)) &&
(!knvlinkIsForcedConfig(pGpu0, pKernelNvlink0)))
{
status = knvlinkRemoveMapping_HAL(pGpu0, pKernelNvlink0, NV_FALSE, NVBIT(peerId),
NV_FALSE /* bL2Entry */);
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%d Failed to remove hshub mapping for peer%d\n",
gpuGetInstance(pGpu0), peerId);
return status;
}
}
//
// Call knvlinkUpdateCurrentConfig to flush settings to the registers
// Skip this call if buffer ready is set and CONFIG_REQUIRE_INITIALIZED is true
//
status = knvlinkSyncLinkMasksAndVbiosInfo(pGpu0, pKernelNvlink0);
if (status != NV_OK)
{
NV_ASSERT(status != NV_OK);
return status;
}
bBufferReady = ((knvlinkGetInitializedLinkMask(pGpu0, pKernelNvlink0) & knvlinkGetPeerLinkMask(pGpu0, pKernelNvlink0, peerId)) != 0) ? NV_TRUE : NV_FALSE;
if (!pKernelNvlink0->getProperty(pKernelNvlink0, PDB_PROP_KNVLINK_CONFIG_REQUIRE_INITIALIZED_LINKS_CHECK) ||
!bBufferReady)
{
status = knvlinkUpdateCurrentConfig(pGpu0, pKernelNvlink0);
}
}
}
return status;
}
/*!
* @brief Remove the P2P mapping to a given peer GPU
*
* @param[in] pGpu0 (local GPU)
* @param[in] pKernelBus0 (local GPU)
* @param[in] pGpu1 (remote GPU)
* @param[in] pKernelBus1 (remote GPU)
* @param[in] peer0 Peer ID (local to remote)
* @param[in] peer1 Peer ID (remote to local)
* @param[in] attributes Sepcial attributes for the mapping
*
* return NV_OK on success
*/
NV_STATUS
kbusRemoveP2PMapping_GP100
(
OBJGPU *pGpu0,
KernelBus *pKernelBus0,
OBJGPU *pGpu1,
KernelBus *pKernelBus1,
NvU32 peer0,
NvU32 peer1,
NvU32 attributes
)
{
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, _NVLINK, attributes) ||
FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, _NVLINK_INDIRECT, attributes))
{
return kbusRemoveP2PMappingForNvlink_HAL(pGpu0, pKernelBus0, pGpu1, pKernelBus1, peer0, peer1, attributes);
}
if (FLD_TEST_DRF(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, _PCIE, attributes))
{
return kbusRemoveP2PMappingForMailbox_HAL(pGpu0, pKernelBus0, pGpu1, pKernelBus1, peer0, peer1, attributes);
}
NV_PRINTF(LEVEL_ERROR, "P2P type %d is not supported\n", DRF_VAL(_P2PAPI, _ATTRIBUTES, _CONNECTION_TYPE, attributes));
return NV_ERR_NOT_SUPPORTED;
}
/*!
* @brief Remove the P2P mapping to a given peer GPU
*
* @param[in] pGpu0 (local GPU)
* @param[in] pKernelBus0 (local GPU)
* @param[in] pGpu1 (remote GPU)
* @param[in] pKernelBus1 (remote GPU)
* @param[in] peer0 Peer ID (local to remote)
* @param[in] peer1 Peer ID (remote to local)
* @param[in] attributes Sepcial attributes for the mapping
*/
NV_STATUS
kbusRemoveP2PMappingForNvlink_GP100
(
OBJGPU *pGpu0,
KernelBus *pKernelBus0,
OBJGPU *pGpu1,
KernelBus *pKernelBus1,
NvU32 peer0,
NvU32 peer1,
NvU32 attributes
)
{
NV_STATUS status = NV_OK;
// If there's no NVLink mapping, fall back to PCIe
if ((pKernelBus0->p2p.busNvlinkPeerNumberMask[pGpu1->gpuInstance] & NVBIT(peer0)) == 0 ||
(pKernelBus1->p2p.busNvlinkPeerNumberMask[pGpu0->gpuInstance] & NVBIT(peer1)) == 0)
{
return NV_ERR_INVALID_STATE;
}
// NVLink mapping exists, remove the NVLink mapping
NV_ASSERT_OK_OR_RETURN(_kbusRemoveNvlinkPeerMapping(pGpu0, pKernelBus0, pGpu1, peer0, attributes));
NV_ASSERT_OK_OR_RETURN(_kbusRemoveNvlinkPeerMapping(pGpu1, pKernelBus1, pGpu0, peer1, attributes));
//
// The P2P mapping for both the GPUs have been destroyed. If the mapping refcount
// for the given peer IDs is zero, then unreserve the peer IDs
//
if ((pKernelBus0->p2p.busNvlinkMappingRefcountPerPeerId[peer0] == 0) &&
pKernelBus0->getProperty(pKernelBus0, PDB_PROP_KBUS_NVLINK_DECONFIG_HSHUB_ON_NO_MAPPING))
{
// Free the reserved peer ID since its no longer used
status = kbusUnreserveP2PPeerIds_HAL(pGpu0, pKernelBus0, NVBIT(peer0));
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%d: Failed to unreserve peer ID mask 0x%x\n",
pGpu0->gpuInstance, NVBIT(peer0));
return status;
}
}
if ((pKernelBus1->p2p.busNvlinkMappingRefcountPerPeerId[peer1] == 0) &&
pKernelBus1->getProperty(pKernelBus1, PDB_PROP_KBUS_NVLINK_DECONFIG_HSHUB_ON_NO_MAPPING))
{
// Free the reserved peer ID since its no longer used
status = kbusUnreserveP2PPeerIds_HAL(pGpu1, pKernelBus1, NVBIT(peer1));
if (status != NV_OK)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%d: Failed to unreserve peer ID mask 0x%x\n",
pGpu1->gpuInstance, NVBIT(peer1));
return status;
}
}
return status;
}
/*!
* @brief Returns the peer number from pGpu (Local) to pGpuPeer
*
* @param[in] pGpu Local
* @param[in] pKernelBus Local
* @param[in] pGpuPeer Remote
*
* @returns NvU32 bus peer number
*/
NvU32
kbusGetPeerId_GP100
(
OBJGPU *pGpu,
KernelBus *pKernelBus,
OBJGPU *pGpuPeer
)
{
extern NvU32 kbusGetPeerId_GM107(OBJGPU *pGpu, KernelBus *pKernelBus, OBJGPU *pPeerGpu);
NvU32 gpuPeerInst = gpuGetInstance(pGpuPeer);
NvU32 peerId = pKernelBus->p2p.busNvlinkPeerNumberMask[gpuPeerInst];
if (peerId == 0)
{
NV_PRINTF(LEVEL_INFO,
"NVLINK P2P not set up between GPU%u and GPU%u, checking for PCIe P2P...\n",
gpuGetInstance(pGpu), gpuPeerInst);
return kbusGetPeerId_GM107(pGpu, pKernelBus, pGpuPeer);
}
LOWESTBITIDX_32(peerId);
return peerId;
}
/**
* @brief Returns if the given peerId is a valid for a given GPU
*
* @param[in] pGpu
* @param[in] pKernelBus
* @param[in] peerId The peer identifier
*
* @return return NV_OK is valid
*/
NV_STATUS
kbusIsPeerIdValid_GP100
(
OBJGPU *pGpu,
KernelBus *pKernelBus,
NvU32 peerId
)
{
extern NV_STATUS kbusIsPeerIdValid_GM107(OBJGPU *pGpu, KernelBus *pKernelBus, NvU32 peerId);
NV_ASSERT_OR_RETURN(peerId < P2P_MAX_NUM_PEERS, NV_ERR_INVALID_INDEX);
if (pKernelBus->p2p.busNvlinkPeerNumberMask[gpuGetInstance(pGpu)] & NVBIT(peerId))
return NV_OK;
return kbusIsPeerIdValid_GM107(pGpu, pKernelBus, peerId);
}
/*!
* @brief Returns the Nvlink peer ID from pGpu0 to pGpu1
*
* @param[in] pGpu0 (local GPU)
* @param[in] pKernelBus0 (local GPU)
* @param[in] pGpu1 (remote GPU)
* @param[in] pKernelBus1 (remote GPU)
* @param[out] nvlinkPeer NvU32 pointer
*
* return NV_OK on success
*/
NV_STATUS
kbusGetNvlinkP2PPeerId_GP100
(
OBJGPU *pGpu0,
KernelBus *pKernelBus0,
OBJGPU *pGpu1,
KernelBus *pKernelBus1,
NvU32 *nvlinkPeer,
NvU32 attributes
)
{
KernelNvlink *pKernelNvlink0 = GPU_GET_KERNEL_NVLINK(pGpu0);
NV_STATUS status = NV_OK;
if (nvlinkPeer == NULL)
{
return NV_ERR_INVALID_ARGUMENT;
}
*nvlinkPeer = BUS_INVALID_PEER;
//
// Use the NVLINK-specific unique ID of the GPU (related to link ID) for
// the peer ID. We expect that this will remain the same across multiple
// runs, so the peer ID should be consistent.
//
// Note: this may not cover peer IDs explicitly requested by the client.
// It is assumed that explicit peer IDs are only used for NVLINK P2P, and
// no PCIe P2P will be used.
//
if ((pKernelNvlink0 != NULL) &&
(knvlinkGetPeersNvlinkMaskFromHshub(pGpu0, pKernelNvlink0) != 0))
{
if (knvlinkIsForcedConfig(pGpu0, pKernelNvlink0) ||
knvlinkAreLinksRegistryOverriden(pGpu0, pKernelNvlink0))
{
*nvlinkPeer = kbusGetPeerIdFromTable_HAL(pGpu0, pKernelBus0,
pGpu0->gpuInstance,
pGpu1->gpuInstance);
if (*nvlinkPeer == BUS_INVALID_PEER)
{
return NV_ERR_INVALID_REQUEST;
}
}
else
{
*nvlinkPeer = kbusGetPeerId_HAL(pGpu0, pKernelBus0, pGpu1);
if (*nvlinkPeer != BUS_INVALID_PEER)
{
return NV_OK;
}
// Reserve GPU0 peer IDs for NVLINK use
if (!pKernelBus0->p2p.bNvlinkPeerIdsReserved)
{
NvU32 idMask = knvlinkGetUniquePeerIdMask_HAL(pGpu0, pKernelNvlink0);
//
// If NVLINK is topology is not forced, idMask will be non-zero
// if nvlinks are detected during topology discovery in core lib
//
if (idMask != 0)
{
// Reserve GPU0 peer IDs for NVLINK use
status = kbusReserveP2PPeerIds_HAL(pGpu0, pKernelBus0, idMask);
if (status != NV_OK)
{
return status;
}
pKernelBus0->p2p.bNvlinkPeerIdsReserved = NV_TRUE;
}
}
*nvlinkPeer = knvlinkGetUniquePeerId_HAL(pGpu0, pKernelNvlink0, pGpu1);
}
}
return status;
}
/*!
* @brief Unreserve peer IDs reserved for nvlink usage
*
* @param[in] pGpu
* @param[in] pKernelBus
* @param[in] peerMask Mask of peer IDs to reserve
*
* return NV_OK on success
*/
NV_STATUS
kbusUnreserveP2PPeerIds_GP100
(
OBJGPU *pGpu,
KernelBus *pKernelBus,
NvU32 peerMask
)
{
NvU32 peerId = 0;
FOR_EACH_INDEX_IN_MASK(32, peerId, peerMask)
{
if (pKernelBus->p2p.busNvlinkMappingRefcountPerPeerId[peerId] > 0)
{
NV_PRINTF(LEVEL_ERROR,
"GPU%u: Cannot unreserve peerId %u. Nvlink refcount > 0\n",
gpuGetInstance(pGpu), peerId);
return NV_ERR_IN_USE;
}
}
FOR_EACH_INDEX_IN_MASK_END;
peerId = 0;
FOR_EACH_INDEX_IN_MASK(32, peerId, peerMask)
{
NV_PRINTF(LEVEL_INFO,
"Unreserving peer ID %u on GPU%u reserved for NVLINK \n",
peerId, gpuGetInstance(pGpu));
if (pKernelBus->p2pPcie.busPeer[peerId].refCount != 0)
{
return NV_ERR_IN_USE;
}
pKernelBus->p2p.bEgmPeer[peerId] = NV_FALSE;
pKernelBus->p2pPcie.busPeer[peerId].bReserved = NV_FALSE;
}
FOR_EACH_INDEX_IN_MASK_END;
return NV_OK;
}
/*!
* @brief Return the NvLink peer number mask for that peer ID
*
* @param[in] pGpu
* @param[in] pKernelBus
* @param[in] peerId peer IDs
*
* return NvU32 Nvlink peer number mask for that peer ID
*/
NvU32
kbusGetNvlinkPeerNumberMask_GP100
(
OBJGPU *pGpu,
KernelBus *pKernelBus,
NvU32 peerId
)
{
if (peerId >= NV_MAX_DEVICES)
{
NV_PRINTF(LEVEL_ERROR,
"Invalid peerId value: %d\n", peerId);
return 0;
}
return (pKernelBus->p2p.busNvlinkPeerNumberMask[peerId]);
}
/*!
* @brief Initialize NV_RAMIN_ADR_LIMIT at the given location
*
* @param[in] pGpu OBJGPU
* @param[in] pKernelBus KernelBus
* @param[in] pBar0Wr Controls whether to MEM_WR using pMap or GPU_REG_WR at addr
* @param[in] instBlockAddr physical address of the instance block to be written
* @param[in] pMap CPU mapped address to start from
* @param[in] vaLimit Virtual address limit to program
*/
void
kbusInstBlkWriteAddrLimit_GP100
(
OBJGPU *pGpu,
KernelBus *pKernelBus,
NvBool bBar0Wr,
NvU64 instBlockAddr,
NvU8 *pMap,
NvU64 vaLimit
)
{
NV_STATUS status = NV_OK;
NvU32 adrLimitLo = (NvU64_LO32(vaLimit) | 0xfff);
NvU32 adrLimitHi = SF_NUM(_RAMIN_ADR_LIMIT, _HI, NvU64_HI32(vaLimit));
if (bBar0Wr)
{
status = kbusMemAccessBar0Window_HAL(pGpu, pKernelBus,
(instBlockAddr + SF_OFFSET(NV_RAMIN_ADR_LIMIT_LO)),
&adrLimitLo,
sizeof(NvU32),
NV_FALSE,
pKernelBus->InstBlkAperture);
NV_ASSERT_OR_RETURN_VOID(NV_OK == status);
status = kbusMemAccessBar0Window_HAL(pGpu, pKernelBus,
(instBlockAddr + SF_OFFSET(NV_RAMIN_ADR_LIMIT_HI)),
&adrLimitHi,
sizeof(NvU32),
NV_FALSE,
pKernelBus->InstBlkAperture);
NV_ASSERT_OR_RETURN_VOID(NV_OK == status);
}
else
{
MEM_WR32(pMap + SF_OFFSET(NV_RAMIN_ADR_LIMIT_LO), adrLimitLo);
MEM_WR32(pMap + SF_OFFSET(NV_RAMIN_ADR_LIMIT_HI), adrLimitHi);
}
}