1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2017 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 #ifndef _NV_LOCK_H_
25 #define _NV_LOCK_H_
26 
27 #include "conftest.h"
28 
29 #include <linux/spinlock.h>
30 #include <linux/rwsem.h>
31 #include <linux/sched.h> /* signal_pending, cond_resched */
32 #include <linux/semaphore.h>
33 
34 #if defined(NV_LINUX_SCHED_SIGNAL_H_PRESENT)
35 #include <linux/sched/signal.h>     /* signal_pending for kernels >= 4.11 */
36 #endif
37 
38 #if defined(CONFIG_PREEMPT_RT) || defined(CONFIG_PREEMPT_RT_FULL)
39 typedef raw_spinlock_t            nv_spinlock_t;
40 #define NV_SPIN_LOCK_INIT(lock)   raw_spin_lock_init(lock)
41 #define NV_SPIN_LOCK_IRQ(lock)    raw_spin_lock_irq(lock)
42 #define NV_SPIN_UNLOCK_IRQ(lock)  raw_spin_unlock_irq(lock)
43 #define NV_SPIN_LOCK_IRQSAVE(lock,flags) raw_spin_lock_irqsave(lock,flags)
44 #define NV_SPIN_UNLOCK_IRQRESTORE(lock,flags) raw_spin_unlock_irqrestore(lock,flags)
45 #define NV_SPIN_LOCK(lock)        raw_spin_lock(lock)
46 #define NV_SPIN_UNLOCK(lock)      raw_spin_unlock(lock)
47 #define NV_SPIN_UNLOCK_WAIT(lock) raw_spin_unlock_wait(lock)
48 #else
49 typedef spinlock_t                nv_spinlock_t;
50 #define NV_SPIN_LOCK_INIT(lock)   spin_lock_init(lock)
51 #define NV_SPIN_LOCK_IRQ(lock)    spin_lock_irq(lock)
52 #define NV_SPIN_UNLOCK_IRQ(lock)  spin_unlock_irq(lock)
53 #define NV_SPIN_LOCK_IRQSAVE(lock,flags) spin_lock_irqsave(lock,flags)
54 #define NV_SPIN_UNLOCK_IRQRESTORE(lock,flags) spin_unlock_irqrestore(lock,flags)
55 #define NV_SPIN_LOCK(lock)        spin_lock(lock)
56 #define NV_SPIN_UNLOCK(lock)      spin_unlock(lock)
57 #define NV_SPIN_UNLOCK_WAIT(lock) spin_unlock_wait(lock)
58 #endif
59 
60 #define NV_INIT_MUTEX(mutex) sema_init(mutex, 1)
61 
62 static inline int nv_down_read_interruptible(struct rw_semaphore *lock)
63 {
64     while (!down_read_trylock(lock))
65     {
66         if (signal_pending(current))
67             return -EINTR;
68         cond_resched();
69     }
70     return 0;
71 }
72 
73 
74 #endif  /* _NV_LOCK_H_ */
75