1 /*
2 * Copyright (C) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "hi_osal.h"
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/printk.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/kallsyms.h>
26
osal_spin_lock_init(osal_spinlock * lock)27 int osal_spin_lock_init(osal_spinlock *lock)
28 {
29 spinlock_t *p = NULL;
30
31 if (lock == NULL) {
32 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
33 return -1;
34 }
35 p = (spinlock_t *)kmalloc(sizeof(spinlock_t) + 8 + 8, GFP_KERNEL); // 8 is malloc size
36 if (p == NULL) {
37 osal_printk("%s - kmalloc error!\n", __FUNCTION__);
38 return -1;
39 }
40 spin_lock_init(p);
41 lock->lock = p;
42 return 0;
43 }
44 EXPORT_SYMBOL(osal_spin_lock_init);
45
osal_spin_lock(osal_spinlock * lock)46 void osal_spin_lock(osal_spinlock *lock)
47 {
48 spinlock_t *p = NULL;
49
50 if (lock == NULL) {
51 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
52 return;
53 }
54
55 p = (spinlock_t *)(lock->lock);
56 spin_lock(p);
57 }
58 EXPORT_SYMBOL(osal_spin_lock);
59
osal_spin_trylock(osal_spinlock * lock)60 int osal_spin_trylock(osal_spinlock *lock)
61 {
62 spinlock_t *p = NULL;
63
64 if (lock == NULL) {
65 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
66 return -1;
67 }
68
69 p = (spinlock_t *)(lock->lock);
70
71 return spin_trylock(p);
72 }
73 EXPORT_SYMBOL(osal_spin_trylock);
74
osal_spin_unlock(osal_spinlock * lock)75 void osal_spin_unlock(osal_spinlock *lock)
76 {
77 spinlock_t *p = NULL;
78
79 if (lock == NULL) {
80 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
81 return;
82 }
83
84 p = (spinlock_t *)(lock->lock);
85 spin_unlock(p);
86 }
87 EXPORT_SYMBOL(osal_spin_unlock);
88
osal_spin_lock_irqsave(osal_spinlock * lock,unsigned long * flags)89 void osal_spin_lock_irqsave(osal_spinlock *lock, unsigned long *flags)
90 {
91 spinlock_t *p = NULL;
92 unsigned long f;
93
94 if ((lock == NULL) || (flags == NULL)) {
95 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
96 return;
97 }
98
99 p = (spinlock_t *)(lock->lock);
100 spin_lock_irqsave(p, f);
101
102 *flags = f;
103 }
104 EXPORT_SYMBOL(osal_spin_lock_irqsave);
105
osal_spin_unlock_irqrestore(osal_spinlock * lock,unsigned long * flags)106 void osal_spin_unlock_irqrestore(osal_spinlock *lock, unsigned long *flags)
107 {
108 spinlock_t *p = NULL;
109
110 if ((lock == NULL) || (flags == NULL)) {
111 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
112 return;
113 }
114
115 p = (spinlock_t *)(lock->lock);
116 spin_unlock_irqrestore(p, *flags);
117 }
118 EXPORT_SYMBOL(osal_spin_unlock_irqrestore);
119
osal_spin_lock_destory(osal_spinlock * lock)120 void osal_spin_lock_destory(osal_spinlock *lock)
121 {
122 spinlock_t *p = NULL;
123
124 if (lock == NULL) {
125 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
126 return;
127 }
128
129 p = (spinlock_t *)(lock->lock);
130 kfree(p);
131 lock->lock = NULL;
132 }
133 EXPORT_SYMBOL(osal_spin_lock_destory);
134