summaryrefslogtreecommitdiff
path: root/components/drivers/hwspinlock/hwspinlock.c
blob: 1e8ca635da5bce27d9747efada84db4061175eba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-23     GuEe-GUI     first version
 */

#include <rtthread.h>

#include <cpuport.h>

#define DBG_TAG "rtdm.hwspinlock"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include "hwspinlock_dm.h"

static RT_DEFINE_SPINLOCK(hwspinlock_ops_lock);
static rt_list_t hwspinlock_bank_nodes = RT_LIST_OBJECT_INIT(hwspinlock_bank_nodes);

rt_err_t rt_hwspinlock_bank_register(struct rt_hwspinlock_bank *bank)
{
    struct rt_hwspinlock *hwlock;

    if (!bank || !bank->ops || bank->locks_nr <= 0 || !bank->dev)
    {
        return -RT_EINVAL;
    }

    rt_list_init(&bank->list);
    rt_ref_init(&bank->ref);

    hwlock = &bank->locks[0];

    for (int i = 0; i < bank->locks_nr; ++i, ++hwlock)
    {
        hwlock->bank = bank;
        hwlock->used = RT_FALSE;
        rt_spin_lock_init(&hwlock->lock);
    }

    rt_spin_lock(&hwspinlock_ops_lock);
    rt_list_insert_after(&hwspinlock_bank_nodes, &bank->list);
    rt_spin_unlock(&hwspinlock_ops_lock);

    rt_dm_dev_bind_fwdata(bank->dev, RT_NULL, bank);

    return RT_EOK;
}

rt_err_t rt_hwspinlock_bank_unregister(struct rt_hwspinlock_bank *bank)
{
    rt_err_t err;

    if (!bank)
    {
        return -RT_EINVAL;
    }

    rt_spin_lock(&hwspinlock_ops_lock);

    if (rt_ref_read(&bank->ref) == 1)
    {
        rt_list_remove(&bank->list);
        rt_dm_dev_unbind_fwdata(bank->dev, RT_NULL);

        err = RT_EOK;
    }
    else
    {
        err = -RT_EBUSY;
    }

    rt_spin_unlock(&hwspinlock_ops_lock);

    return err;
}

rt_err_t rt_hwspin_trylock_raw(struct rt_hwspinlock *hwlock,
        rt_ubase_t *out_irq_level)
{
    rt_err_t err;

    if (!hwlock)
    {
        return -RT_EINVAL;
    }

    if (out_irq_level)
    {
        *out_irq_level = rt_spin_lock_irqsave(&hwlock->lock);
    }
    else
    {
        rt_spin_lock(&hwlock->lock);
    }

    err = hwlock->bank->ops->trylock(hwlock);

    if (err)
    {
        if (out_irq_level)
        {
            rt_spin_unlock_irqrestore(&hwlock->lock, *out_irq_level);
        }
        else
        {
            rt_spin_unlock(&hwlock->lock);
        }
    }

    rt_hw_dmb();

    return err;
}

rt_err_t rt_hwspin_lock_timeout_raw(struct rt_hwspinlock *hwlock,
        rt_uint32_t timeout_ms, rt_ubase_t *out_irq_level)
{
    rt_err_t err;
    rt_tick_t timeout = rt_tick_get() + rt_tick_from_millisecond(timeout_ms);

    for (;;)
    {
        err = rt_hwspin_trylock_raw(hwlock, out_irq_level);

        if (err != -RT_EBUSY)
        {
            break;
        }

        if (timeout < rt_tick_get())
        {
            return -RT_ETIMEOUT;
        }

        if (hwlock->bank->ops->relax)
        {
            hwlock->bank->ops->relax(hwlock);
        }
    }

    return err;
}

void rt_hwspin_unlock_raw(struct rt_hwspinlock *hwlock,
        rt_ubase_t *out_irq_level)
{
    if (!hwlock)
    {
        return;
    }

    rt_hw_dmb();

    hwlock->bank->ops->unlock(hwlock);

    if (out_irq_level)
    {
        rt_spin_unlock_irqrestore(&hwlock->lock, *out_irq_level);
    }
    else
    {
        rt_spin_unlock(&hwlock->lock);
    }
}

static struct rt_hwspinlock *hwspinlock_get(struct rt_hwspinlock_bank *bank, int id)
{
    struct rt_hwspinlock *hwlock = RT_NULL;

    if (bank)
    {
        int offset = id - bank->base_id;

        if (!bank->locks[offset].used)
        {
            hwlock = &bank->locks[offset];
        }
    }
    else
    {
        rt_list_for_each_entry(bank, &hwspinlock_bank_nodes, list)
        {
            hwlock = rt_err_ptr(-RT_EBUSY);

            for (int i = 0; i < bank->locks_nr; ++i)
            {
                if (!bank->locks[i].used)
                {
                    hwlock = &bank->locks[i];
                    goto _found;
                }
            }
        }
    }

_found:
    if (!rt_is_err_or_null(hwlock))
    {
        hwlock->used = RT_TRUE;
        rt_ref_get(&hwlock->bank->ref);
    }

    return hwlock;
}

struct rt_hwspinlock *rt_hwspinlock_get(void)
{
    struct rt_hwspinlock *lock;

    rt_spin_lock(&hwspinlock_ops_lock);

    lock = hwspinlock_get(RT_NULL, -1);

    rt_spin_unlock(&hwspinlock_ops_lock);

    return lock;
}

struct rt_hwspinlock *rt_hwspinlock_get_by_index(struct rt_device *dev, int index)
{
    return rt_ofw_get_hwspinlock_by_index(dev->ofw_node, index);
}

struct rt_hwspinlock *rt_hwspinlock_get_by_name(struct rt_device *dev, const char *name)
{
    return rt_ofw_get_hwspinlock_by_name(dev->ofw_node, name);
}

static void hwspinlock_release(struct rt_ref *r)
{
    struct rt_hwspinlock_bank *bank = rt_container_of(r, struct rt_hwspinlock_bank, ref);

    LOG_E("%s is release", rt_dm_dev_get_name(bank->dev));
    (void)bank;

    RT_ASSERT(0);
}

void rt_hwspinlock_put(struct rt_hwspinlock *hwlock)
{
    if (hwlock)
    {
        rt_spin_lock(&hwspinlock_ops_lock);
        hwlock->used = RT_FALSE;
        rt_spin_unlock(&hwspinlock_ops_lock);

        rt_ref_put(&hwlock->bank->ref, &hwspinlock_release);
    }
}

struct rt_hwspinlock *rt_ofw_get_hwspinlock_by_index(struct rt_ofw_node *np, int index)
{
    rt_err_t err;
    struct rt_ofw_node *bank_np;
    struct rt_ofw_cell_args args;
    struct rt_hwspinlock *lock;
    struct rt_hwspinlock_bank *bank;

    if (!np || index < 0)
    {
        return rt_err_ptr(-RT_EINVAL);
    }

    err = rt_ofw_parse_phandle_cells(np, "hwlocks", "#hwlock-cells", index, &args);

    if (err)
    {
        return rt_err_ptr(err);
    }

    bank_np = args.data;

    if (!rt_ofw_data(bank_np))
    {
        rt_platform_ofw_request(bank_np);
    }

    rt_spin_lock(&hwspinlock_ops_lock);

    bank = rt_ofw_data(bank_np);
    rt_ofw_node_put(bank_np);

    if (!bank || args.args_count != 1)
    {
        lock = rt_err_ptr(-RT_ENOSYS);
    }
    else
    {
        lock = hwspinlock_get(bank, bank->base_id + args.args[0]);
    }

    rt_spin_unlock(&hwspinlock_ops_lock);

    return lock;
}

struct rt_hwspinlock *rt_ofw_get_hwspinlock_by_name(struct rt_ofw_node *np, const char *name)
{
    int index;

    if (!np || !name)
    {
        return rt_err_ptr(-RT_EINVAL);
    }

    index = rt_ofw_prop_index_of_string(np, "hwlock-names", name);

    if (index < 0)
    {
        return rt_err_ptr(index);
    }

    return rt_ofw_get_hwspinlock_by_index(np, index);
}