1 /*
2 * Copyright (C) 2021 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
osal_spin_lock_init(osal_spinlock_t * lock)26 int osal_spin_lock_init(osal_spinlock_t *lock)
27 {
28 spinlock_t *p = NULL;
29
30 if (lock == NULL) {
31 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
32 return -1;
33 }
34 p = (spinlock_t *)kmalloc(sizeof(spinlock_t), GFP_KERNEL);
35 if (p == NULL) {
36 osal_trace("%s - kmalloc error!\n", __FUNCTION__);
37 return -1;
38 }
39 spin_lock_init(p);
40 lock->lock = p;
41 return 0;
42 }
43 EXPORT_SYMBOL(osal_spin_lock_init);
osal_spin_lock(osal_spinlock_t * lock)44 void osal_spin_lock(osal_spinlock_t *lock)
45 {
46 spinlock_t *p = NULL;
47
48 p = (spinlock_t *)(lock->lock);
49 spin_lock(p);
50 }
51 EXPORT_SYMBOL(osal_spin_lock);
osal_spin_trylock(osal_spinlock_t * lock)52 int osal_spin_trylock(osal_spinlock_t *lock)
53 {
54 spinlock_t *p = NULL;
55
56 if (lock == NULL) {
57 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
58 return -1;
59 }
60 p = (spinlock_t *)(lock->lock);
61 return spin_trylock(p);
62 }
63 EXPORT_SYMBOL(osal_spin_trylock);
osal_spin_unlock(osal_spinlock_t * lock)64 void osal_spin_unlock(osal_spinlock_t *lock)
65 {
66 spinlock_t *p = NULL;
67
68 p = (spinlock_t *)(lock->lock);
69 spin_unlock(p);
70 }
71 EXPORT_SYMBOL(osal_spin_unlock);
osal_spin_lock_irqsave(osal_spinlock_t * lock,unsigned long * flags)72 void osal_spin_lock_irqsave(osal_spinlock_t *lock, unsigned long *flags)
73 {
74 spinlock_t *p = NULL;
75 unsigned long f;
76
77 p = (spinlock_t *)(lock->lock);
78 spin_lock_irqsave(p, f);
79 *flags = f;
80 }
81 EXPORT_SYMBOL(osal_spin_lock_irqsave);
osal_spin_unlock_irqrestore(osal_spinlock_t * lock,const unsigned long * flags)82 void osal_spin_unlock_irqrestore(osal_spinlock_t *lock, const unsigned long *flags)
83 {
84 spinlock_t *p = NULL;
85 unsigned long f;
86
87 p = (spinlock_t *)(lock->lock);
88 f = *flags;
89 spin_unlock_irqrestore(p, f);
90 }
91 EXPORT_SYMBOL(osal_spin_unlock_irqrestore);
osal_spin_lock_destroy(osal_spinlock_t * lock)92 void osal_spin_lock_destroy(osal_spinlock_t *lock)
93 {
94 spinlock_t *p = NULL;
95
96 p = (spinlock_t *)(lock->lock);
97 kfree(p);
98 lock->lock = NULL;
99 }
100 EXPORT_SYMBOL(osal_spin_lock_destroy);
101