OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [src/] [arch/] [arm/] [v6/] [mutex.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Copyright (C) 2009 B Labs Ltd.
3
 */
4
 
5
#include <l4/macros.h>
6
#include <l4/lib/printk.h>
7
#include <l4/lib/mutex.h>
8
#include INC_SUBARCH(mmu_ops.h)
9
 
10
#define MUTEX_UNLOCKED  0
11
#define MUTEX_LOCKED    1
12
 
13
 
14
/* Notes on ldrex/strex:
15
 * ldrex rD, [rN, #imm] : loads rD with contents of at address (rN + imm)
16
 * strex rD, rS, [rN, #imm]: pushes contents of rS to memory location (rN + imm)
17
 *              rD is 0 if operation is successful, 1 otherwise
18
 */
19
 
20
void __spin_lock(unsigned int *s)
21
{
22
        unsigned int tmp;
23
        __asm__ __volatile__ (
24
                "1:\n"
25
                "ldrex    %0, [%2]\n"
26
                "teq      %0, #0\n"
27
                "strexeq  %0, %1, [%2]\n"
28
                "teq      %0, #0\n"
29
#ifdef CONFIG_SMP
30
                "wfene\n"
31
#endif
32
                "bne      1b\n"
33
                : "=&r" (tmp)
34
                : "r"(1), "r"(s)
35
                : "cc", "memory"
36
        );
37
 
38
        dsb();
39
}
40
 
41
void __spin_unlock(unsigned int *s)
42
{
43
        __asm__ __volatile__ (
44
                "str    %0, [%1]\n"
45
                :
46
                : "r"(0), "r"(s)
47
                : "memory"
48
        );
49
 
50
#ifdef CONFIG_SMP
51
        dsb();
52
        __asm__ __volatile__ ("sev\n");
53
#endif
54
}
55
 
56
 
57
/*
58
 * Current implementation uses __mutex_(un)lock within a protected
59
 * spinlock, needs to be revisited in the future
60
 */
61
unsigned int __mutex_lock(unsigned int *m)
62
{
63
        unsigned int tmp, res;
64
        __asm__ __volatile__ (
65
                "1:\n"
66
                "ldrex  %0, [%3]\n"
67
                "tst    %0, #0\n"
68
                "strexeq %1, %2, [%3]\n"
69
                "tsteq  %1, #0\n"
70
                "bne 1b\n"
71
                : "=&r" (tmp), "=&r"(res)
72
                : "r"(1), "r"(m)
73
                : "cc", "memory"
74
        );
75
 
76
        if ((tmp | res) != 0)
77
                return 0;
78
        return 1;
79
}
80
 
81
void __mutex_unlock(unsigned int *m)
82
{
83
        __asm__ __volatile__ (
84
                "str  %0, [%1] \n"
85
                :
86
                : "r"(0), "r"(m)
87
                : "memory"
88
                );
89
 
90
}
91
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.