1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 
25 #ifndef _NVLINK_LOCK_H_
26 #define _NVLINK_LOCK_H_
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include "nvlink_common.h"
33 
34 /*
35  * Allocate top level lock. Return NVL_SUCCESS if
36  * the lock was allocated else return NVL_ERR_GENERIC.
37  */
38 NvlStatus nvlink_lib_top_lock_alloc(void);
39 
40 /*
41  * Free top level lock. Return NVL_SUCCESS if
42  * the lock was freed else return NVL_ERR_GENERIC.
43  */
44 NvlStatus nvlink_lib_top_lock_free(void);
45 
46 /*
47  * Allocate per-link lock. Return NVL_SUCCESS if
48  * the lock was allocated else return NVL_ERR_GENERIC.
49  */
50 NvlStatus nvlink_lib_link_lock_alloc(nvlink_link *link);
51 
52 /*
53  * Free per-link lock. Return NVL_SUCCESS if
54  * the lock was freed else return NVL_ERR_GENERIC.
55  */
56 NvlStatus nvlink_lib_link_lock_free(nvlink_link *link);
57 
58 /*
59  * Acquire top level lock. Return NVL_SUCCESS if
60  * the lock was acquired else return NVL_ERR_STATE_IN_USE.
61  */
62 NvlStatus nvlink_lib_top_lock_acquire(void);
63 
64 /*
65  * Release top level lock. Return NVL_SUCCESS if
66  * the lock was released else return NVL_ERR_GENERIC.
67  */
68 NvlStatus nvlink_lib_top_lock_release(void);
69 
70 /*
71  * Sort the array of links in order of (DBDF, link#) -
72  * lowest to highest and acquire link locks.
73  * Return NVL_SUCCESS if all the link locks were acquired.
74  * Else if any link lock failed to be acquired, release
75  * all acquired link locks and return NVL_ERR_STATE_IN_USE.
76  */
77 NvlStatus nvlink_lib_link_locks_acquire(nvlink_link **links, int numLinks);
78 
79 /*
80  * Loop over all the links and call nvlink_releaseLock(links[i]->linkLock).
81  * Return NVL_SUCCESS if all the link locks were released.
82  * Else if any link lock failed to be released return NVL_ERR_GENERIC.
83  */
84 NvlStatus nvlink_lib_link_locks_release(nvlink_link **links, int numLinks);
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #endif // _NVLINK_LOCK_H_
91