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

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [libl4/] [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) 2010 B Labs Ltd.
3
 * Author: Prem Mallappa <prem.mallappa@b-labs.co.uk>
4
 */
5
 
6
#include <l4lib/mutex.h>
7
#include <l4lib/types.h>
8
#include L4LIB_INC_ARCH(syslib.h)       /* for BUG/BUG_ON,  */
9
#include L4LIB_INC_ARCH(asm.h)
10
#include INC_SUBARCH(mmu_ops.h)
11
 
12
int __l4_mutex_lock(void *m, l4id_t tid)
13
{
14
        int tmp, ret;
15
 loop:
16
        __asm__ __volatile__(
17
                             "ldrex %0, [%1]\n"
18
                             : "=&r"(tmp)
19
                             : "r"(m)
20
                             : "memory"
21
        );
22
 
23
        if(tmp != L4_MUTEX_UNLOCKED)
24
                ret = L4_MUTEX_CONTENDED;
25
        else
26
                ret = L4_MUTEX_SUCCESS;
27
 
28
        /* Store our 'tid' */
29
        __asm__ __volatile__(
30
                             "strex     %0, %1, [%2]\n"
31
                             :"=&r"(tmp)
32
                             :"r"(tid), "r"(m)
33
                             );
34
        if (tmp != 0) {
35
                /* We couldn't succeed the store, we retry */
36
#ifdef CONFIG_SMP
37
                  /* don't hog the CPU, sleep till an event */
38
                __asm__ __volatile__("wfe\n");
39
#endif
40
                goto loop;
41
        }
42
 
43
        dsb();
44
 
45
        return ret;
46
}
47
 
48
int __l4_mutex_unlock(void *m, l4id_t tid)
49
{
50
        int tmp, ret;
51
 loop:
52
        /* Load and see if the lock had our tid */
53
        __asm__ __volatile__(
54
                             "ldrex %0, [%1]\n"
55
                             : "=r"(tmp)
56
                             : "r"(m)
57
                             );
58
 
59
        if(tmp != tid)
60
                ret = L4_MUTEX_CONTENDED;
61
        else
62
                ret = L4_MUTEX_SUCCESS;
63
 
64
        /* We store unlock value '0' */
65
        __asm__ __volatile__(
66
                             "strex     %0, %1, [%2]\n"
67
                             :"=&r"(tmp)
68
                             :"rI"(L4_MUTEX_UNLOCKED), "r"(m)
69
                             );
70
        if(tmp != 0) {
71
                /* The store wasn't successfull, retry */
72
                goto loop;
73
        }
74
 
75
        dsb();
76
 
77
#ifdef CONFIG_SMP
78
        __asm__ __volatile__("sev\n");
79
#endif
80
        return ret;
81
}
82
 
83
u8 l4_atomic_dest_readb(unsigned long *location)
84
{
85
        unsigned int tmp, res;
86
        __asm__ __volatile__ (
87
                "1:                             \n"
88
                "       ldrex %0, [%2]          \n"
89
                "       strex %1, %3, [%2]      \n"
90
                "       teq %1, #0              \n"
91
                "       bne 1b                  \n"
92
                : "=&r"(tmp), "=&r"(res)
93
                : "r"(location), "r"(0)
94
                : "cc", "memory"
95
        );
96
 
97
        return (u8)tmp;
98
}

powered by: WebSVN 2.1.0

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