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 <asm/atomic.h>
23 #include <linux/printk.h>
24 #include <linux/slab.h>
25 #include "securec.h"
26
osal_atomic_init(osal_atomic_t * atomic)27 int osal_atomic_init(osal_atomic_t *atomic)
28 {
29 atomic_t *p = NULL;
30
31 if (atomic == NULL) {
32 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
33 return -1;
34 }
35 p = (atomic_t *)kmalloc(sizeof(atomic_t), GFP_KERNEL);
36 if (p == NULL) {
37 osal_trace("%s - kmalloc error!\n", __FUNCTION__);
38 return -1;
39 }
40 (void)memset_s(p, sizeof(atomic_t), 0, sizeof(atomic_t));
41 atomic->atomic = p;
42 return 0;
43 }
44 EXPORT_SYMBOL(osal_atomic_init);
osal_atomic_destroy(osal_atomic_t * atomic)45 void osal_atomic_destroy(osal_atomic_t *atomic)
46 {
47 if (atomic != NULL) {
48 kfree(atomic->atomic);
49 atomic->atomic = NULL;
50 }
51 }
52 EXPORT_SYMBOL(osal_atomic_destroy);
osal_atomic_read(osal_atomic_t * atomic)53 int osal_atomic_read(osal_atomic_t *atomic)
54 {
55 atomic_t *p = NULL;
56
57 if (atomic == NULL) {
58 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
59 return -1;
60 }
61 p = (atomic_t *)(atomic->atomic);
62 return atomic_read(p);
63 }
64 EXPORT_SYMBOL(osal_atomic_read);
osal_atomic_set(osal_atomic_t * atomic,int i)65 void osal_atomic_set(osal_atomic_t *atomic, int i)
66 {
67 atomic_t *p = NULL;
68 if (atomic != NULL) {
69 p = (atomic_t *)(atomic->atomic);
70 atomic_set(p, i);
71 }
72 }
73 EXPORT_SYMBOL(osal_atomic_set);
osal_atomic_inc_return(osal_atomic_t * atomic)74 int osal_atomic_inc_return(osal_atomic_t *atomic)
75 {
76 atomic_t *p = NULL;
77
78 if (atomic == NULL) {
79 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
80 return -1;
81 }
82 p = (atomic_t *)(atomic->atomic);
83 return atomic_inc_return(p);
84 }
85 EXPORT_SYMBOL(osal_atomic_inc_return);
osal_atomic_dec_return(osal_atomic_t * atomic)86 int osal_atomic_dec_return(osal_atomic_t *atomic)
87 {
88 atomic_t *p = NULL;
89
90 if (atomic == NULL) {
91 osal_trace("%s - parameter invalid!\n", __FUNCTION__);
92 return -1;
93 }
94 p = (atomic_t *)(atomic->atomic);
95 return atomic_dec_return(p);
96 }
97 EXPORT_SYMBOL(osal_atomic_dec_return);
98