blob: d3c780cbb28c7791525ce7a769e5d4904c19869a [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
2 * Copyright (c) 2013, ARM Limited. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <assert.h>
32#include <string.h>
33
34#include <bakery_lock.h>
35
36#define assert_bakery_entry_valid(entry, bakery) do { \
37 assert(bakery); \
38 assert(entry < BAKERY_LOCK_MAX_CPUS); \
39} while(0)
40
41void bakery_lock_init(bakery_lock * bakery)
42{
43 assert(bakery);
44 memset(bakery, 0, sizeof(*bakery));
45 bakery->owner = NO_OWNER;
46}
47
48void bakery_lock_get(unsigned long mpidr, bakery_lock * bakery)
49{
50 unsigned int i, max = 0, my_full_number, his_full_number, entry;
51
52 entry = platform_get_core_pos(mpidr);
53
54 assert_bakery_entry_valid(entry, bakery);
55
56 // Catch recursive attempts to take the lock under the same entry:
57 assert(bakery->owner != entry);
58
59 // Get a ticket
60 bakery->entering[entry] = 1;
61 for (i = 0; i < BAKERY_LOCK_MAX_CPUS; ++i) {
62 if (bakery->number[i] > max) {
63 max = bakery->number[i];
64 }
65 }
66 ++max;
67 bakery->number[entry] = max;
68 bakery->entering[entry] = 0;
69
70 // Wait for our turn
71 my_full_number = (max << 8) + entry;
72 for (i = 0; i < BAKERY_LOCK_MAX_CPUS; ++i) {
73 while (bakery->entering[i]) ; /* Wait */
74 do {
75 his_full_number = bakery->number[i];
76 if (his_full_number) {
77 his_full_number = (his_full_number << 8) + i;
78 }
79 }
80 while (his_full_number && (his_full_number < my_full_number));
81 }
82
83 bakery->owner = entry;
84}
85
86void bakery_lock_release(unsigned long mpidr, bakery_lock * bakery)
87{
88 unsigned int entry = platform_get_core_pos(mpidr);
89
90 assert_bakery_entry_valid(entry, bakery);
91 assert(bakery_lock_held(entry, bakery));
92
93 bakery->owner = NO_OWNER;
94 bakery->number[entry] = 0;
95}
96
97int bakery_lock_held(unsigned long mpidr, const bakery_lock * bakery)
98{
99 unsigned int entry = platform_get_core_pos(mpidr);
100
101 assert_bakery_entry_valid(entry, bakery);
102
103 return bakery->owner == entry;
104}