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 <linux/module.h>
20 #include <linux/kernel.h>
21 #include <asm/atomic.h>
22 #include <linux/printk.h>
23 #include <linux/slab.h>
24 #include "hi_osal.h"
25 #include "securec.h"
26
osal_atomic_init(osal_atomic * atomic)27 int osal_atomic_init(osal_atomic *atomic)
28 {
29 errno_t err;
30 atomic_t *p = NULL;
31
32 if (atomic == NULL) {
33 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
34 return -1;
35 }
36
37 p = (atomic_t *)kmalloc(sizeof(atomic_t), GFP_KERNEL);
38 if (p == NULL) {
39 osal_printk("%s - kmalloc error!\n", __FUNCTION__);
40 return -1;
41 }
42
43 err = memset_s(p, sizeof(atomic_t), 0, sizeof(atomic_t));
44 if (err != EOK) {
45 kfree(p);
46 p = NULL;
47 osal_printk("memset_s is failed.\n");
48 return -1;
49 }
50
51 atomic->atomic = p;
52 return 0;
53 }
54 EXPORT_SYMBOL(osal_atomic_init);
55
osal_atomic_destory(osal_atomic * atomic)56 void osal_atomic_destory(osal_atomic *atomic)
57 {
58 if (atomic == NULL) {
59 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
60 return;
61 }
62
63 kfree(atomic->atomic);
64 atomic->atomic = NULL;
65 }
66 EXPORT_SYMBOL(osal_atomic_destory);
67
osal_atomic_read(osal_atomic * atomic)68 int osal_atomic_read(osal_atomic *atomic)
69 {
70 atomic_t *p = NULL;
71
72 if (atomic == NULL) {
73 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
74 return -1;
75 }
76
77 p = (atomic_t *)(atomic->atomic);
78
79 return atomic_read(p);
80 }
81 EXPORT_SYMBOL(osal_atomic_read);
82
osal_atomic_set(osal_atomic * atomic,int i)83 void osal_atomic_set(osal_atomic *atomic, int i)
84 {
85 atomic_t *p = NULL;
86
87 if (atomic == NULL) {
88 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
89 return;
90 }
91
92 p = (atomic_t *)(atomic->atomic);
93 atomic_set(p, i);
94 }
95 EXPORT_SYMBOL(osal_atomic_set);
96
osal_atomic_inc_return(osal_atomic * atomic)97 int osal_atomic_inc_return(osal_atomic *atomic)
98 {
99 atomic_t *p = NULL;
100
101 if (atomic == NULL) {
102 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
103 return -1;
104 }
105
106 p = (atomic_t *)(atomic->atomic);
107
108 return atomic_inc_return(p);
109 }
110 EXPORT_SYMBOL(osal_atomic_inc_return);
111
osal_atomic_dec_return(osal_atomic * atomic)112 int osal_atomic_dec_return(osal_atomic *atomic)
113 {
114 atomic_t *p = NULL;
115
116 if (atomic == NULL) {
117 osal_printk("%s - parameter invalid!\n", __FUNCTION__);
118 return -1;
119 }
120
121 p = (atomic_t *)(atomic->atomic);
122
123 return atomic_dec_return(p);
124 }
125 EXPORT_SYMBOL(osal_atomic_dec_return);
126