Skip to content

Commit 9d8cefe

Browse files
committed
[dm][hwspinlock] support hwspinlock
Hardware spinlock modules provide hardware assistance for synchronization and mutual exclusion between heterogeneous processors and those not operating under a single, shared operating system. Signed-off-by: GuEe-GUI <[email protected]>
1 parent 2fb53c8 commit 9d8cefe

File tree

6 files changed

+405
-0
lines changed

6 files changed

+405
-0
lines changed

components/drivers/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ rsource "hwcrypto/Kconfig"
2323
rsource "wlan/Kconfig"
2424
rsource "led/Kconfig"
2525
rsource "mailbox/Kconfig"
26+
rsource "hwspinlock/Kconfig"
2627
rsource "phye/Kconfig"
2728
rsource "ata/Kconfig"
2829
rsource "nvme/Kconfig"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
menuconfig RT_USING_HWSPINLOCK
2+
bool "Using Hardware Spinlock device drivers"
3+
depends on RT_USING_DM
4+
depends on RT_USING_OFW
5+
select RT_USING_ADT
6+
select RT_USING_ADT_REF
7+
default n
8+
help
9+
Hardware spinlock modules provide hardware assistance for
10+
synchronization and mutual exclusion between heterogeneous processors
11+
and those not operating under a single, shared operating system.
12+
13+
if RT_USING_HWSPINLOCK
14+
osource "$(SOC_DM_HWSPINLOCK_DIR)/Kconfig"
15+
endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from building import *
2+
3+
group = []
4+
5+
if not GetDepend(['RT_USING_HWSPINLOCK']):
6+
Return('group')
7+
8+
cwd = GetCurrentDir()
9+
CPPPATH = [cwd + '/../include']
10+
11+
src = ['hwspinlock.c']
12+
13+
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
14+
15+
Return('group')
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
/*
2+
* Copyright (c) 2006-2023, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2023-09-23 GuEe-GUI first version
9+
*/
10+
11+
#include <rtthread.h>
12+
13+
#include <cpuport.h>
14+
15+
#define DBG_TAG "rtdm.hwspinlock"
16+
#define DBG_LVL DBG_INFO
17+
#include <rtdbg.h>
18+
19+
#include "hwspinlock_dm.h"
20+
21+
static RT_DEFINE_SPINLOCK(hwspinlock_ops_lock);
22+
static rt_list_t hwspinlock_bank_nodes = RT_LIST_OBJECT_INIT(hwspinlock_bank_nodes);
23+
24+
rt_err_t rt_hwspinlock_bank_register(struct rt_hwspinlock_bank *bank)
25+
{
26+
struct rt_hwspinlock *hwlock;
27+
28+
if (!bank || !bank->ops || bank->locks_nr <= 0 || !bank->dev)
29+
{
30+
return -RT_EINVAL;
31+
}
32+
33+
rt_list_init(&bank->list);
34+
rt_ref_init(&bank->ref);
35+
36+
hwlock = &bank->locks[0];
37+
38+
for (int i = 0; i < bank->locks_nr; ++i, ++hwlock)
39+
{
40+
rt_spin_lock_init(&hwlock->lock);
41+
hwlock->used = RT_FALSE;
42+
}
43+
44+
rt_spin_lock(&hwspinlock_ops_lock);
45+
rt_list_insert_after(&hwspinlock_bank_nodes, &bank->list);
46+
rt_spin_unlock(&hwspinlock_ops_lock);
47+
48+
rt_dm_dev_bind_fwdata(bank->dev, RT_NULL, bank);
49+
50+
return RT_EOK;
51+
}
52+
53+
rt_err_t rt_hwspinlock_bank_unregister(struct rt_hwspinlock_bank *bank)
54+
{
55+
rt_err_t err;
56+
57+
if (!bank)
58+
{
59+
return -RT_EINVAL;
60+
}
61+
62+
rt_spin_lock(&hwspinlock_ops_lock);
63+
64+
if (rt_ref_read(&bank->ref) == 1)
65+
{
66+
rt_dm_dev_unbind_fwdata(bank->dev, RT_NULL);
67+
68+
err = RT_EOK;
69+
}
70+
else
71+
{
72+
err = -RT_EBUSY;
73+
}
74+
75+
rt_spin_unlock(&hwspinlock_ops_lock);
76+
77+
return err;
78+
}
79+
80+
rt_err_t rt_hwspin_trylock_raw(struct rt_hwspinlock *hwlock,
81+
rt_ubase_t *out_irq_level)
82+
{
83+
rt_err_t err;
84+
85+
if (!hwlock)
86+
{
87+
return -RT_EINVAL;
88+
}
89+
90+
if (out_irq_level)
91+
{
92+
*out_irq_level = rt_spin_lock_irqsave(&hwlock->lock);
93+
}
94+
else
95+
{
96+
rt_spin_lock(&hwlock->lock);
97+
}
98+
99+
err = hwlock->bank->ops->trylock(hwlock);
100+
101+
if (err)
102+
{
103+
if (out_irq_level)
104+
{
105+
rt_spin_unlock_irqrestore(&hwlock->lock, *out_irq_level);
106+
}
107+
else
108+
{
109+
rt_spin_unlock(&hwlock->lock);
110+
}
111+
}
112+
113+
rt_hw_dmb();
114+
115+
return err;
116+
}
117+
118+
rt_err_t rt_hwspin_lock_timeout_raw(struct rt_hwspinlock *hwlock,
119+
rt_uint32_t timeout_ms, rt_ubase_t *out_irq_level)
120+
{
121+
rt_err_t err;
122+
rt_tick_t timeout = rt_tick_get() + rt_tick_from_millisecond(timeout_ms);
123+
124+
for (;;)
125+
{
126+
err = rt_hwspin_trylock_raw(hwlock, out_irq_level);
127+
128+
if (err != -RT_EBUSY)
129+
{
130+
break;
131+
}
132+
133+
if (timeout < rt_tick_get())
134+
{
135+
return -RT_ETIMEOUT;
136+
}
137+
138+
if (hwlock->bank->ops->relax)
139+
{
140+
hwlock->bank->ops->relax(hwlock);
141+
}
142+
}
143+
144+
return err;
145+
}
146+
147+
void rt_hwspin_unlock_raw(struct rt_hwspinlock *hwlock,
148+
rt_ubase_t *out_irq_level)
149+
{
150+
if (!hwlock)
151+
{
152+
return;
153+
}
154+
155+
rt_hw_dmb();
156+
157+
hwlock->bank->ops->unlock(hwlock);
158+
159+
if (out_irq_level)
160+
{
161+
rt_spin_unlock_irqrestore(&hwlock->lock, *out_irq_level);
162+
}
163+
else
164+
{
165+
rt_spin_unlock(&hwlock->lock);
166+
}
167+
}
168+
169+
static struct rt_hwspinlock *hwspinlock_get(struct rt_hwspinlock_bank *bank, int id)
170+
{
171+
struct rt_hwspinlock *hwlock = RT_NULL;
172+
173+
if (bank)
174+
{
175+
int offset = id - bank->base_id;
176+
177+
if (!bank->locks[offset].used)
178+
{
179+
hwlock = &bank->locks[offset];
180+
}
181+
}
182+
else
183+
{
184+
rt_list_for_each_entry(bank, &hwspinlock_bank_nodes, list)
185+
{
186+
hwlock = rt_err_ptr(-RT_EBUSY);
187+
188+
for (int i = 0; i < bank->locks_nr; ++i)
189+
{
190+
if (!bank->locks[i].used)
191+
{
192+
hwlock = &bank->locks[i];
193+
goto _found;
194+
}
195+
}
196+
}
197+
}
198+
199+
_found:
200+
if (!rt_is_err_or_null(hwlock))
201+
{
202+
hwlock->used = RT_TRUE;
203+
}
204+
205+
return hwlock;
206+
}
207+
208+
struct rt_hwspinlock *rt_hwspinlock_get(void)
209+
{
210+
struct rt_hwspinlock *lock;
211+
212+
rt_spin_lock(&hwspinlock_ops_lock);
213+
214+
lock = hwspinlock_get(RT_NULL, -1);
215+
216+
rt_spin_unlock(&hwspinlock_ops_lock);
217+
218+
return lock;
219+
}
220+
221+
struct rt_hwspinlock *rt_hwspinlock_get_by_index(struct rt_device *dev, int index)
222+
{
223+
return rt_ofw_get_hwspinlock_by_index(dev->ofw_node, index);
224+
}
225+
226+
struct rt_hwspinlock *rt_hwspinlock_get_by_name(struct rt_device *dev, const char *name)
227+
{
228+
return rt_ofw_get_hwspinlock_by_name(dev->ofw_node, name);
229+
}
230+
231+
static void hwspinlock_release(struct rt_ref *r)
232+
{
233+
struct rt_hwspinlock_bank *bank = rt_container_of(r, struct rt_hwspinlock_bank, ref);
234+
235+
LOG_E("%s is release", rt_dm_dev_get_name(bank->dev));
236+
(void)bank;
237+
238+
RT_ASSERT(0);
239+
}
240+
241+
void rt_hwspinlock_put(struct rt_hwspinlock *hwlock)
242+
{
243+
if (hwlock)
244+
{
245+
rt_ref_put(&hwlock->bank->ref, &hwspinlock_release);
246+
hwlock->used = RT_TRUE;
247+
}
248+
}
249+
250+
struct rt_hwspinlock *rt_ofw_get_hwspinlock_by_index(struct rt_ofw_node *np, int index)
251+
{
252+
rt_err_t err;
253+
struct rt_ofw_node *bank_np;
254+
struct rt_ofw_cell_args args;
255+
struct rt_hwspinlock *lock;
256+
struct rt_hwspinlock_bank *bank;
257+
258+
if (!np && index < 0)
259+
{
260+
return rt_err_ptr(-RT_EINVAL);
261+
}
262+
263+
err = rt_ofw_parse_phandle_cells(np, "hwlocks", "#hwlock-cells", index, &args);
264+
265+
if (err)
266+
{
267+
return rt_err_ptr(err);
268+
}
269+
270+
bank_np = args.data;
271+
272+
if (!rt_ofw_data(bank_np))
273+
{
274+
rt_platform_ofw_request(bank_np);
275+
}
276+
277+
rt_spin_lock(&hwspinlock_ops_lock);
278+
279+
bank = rt_ofw_data(bank_np);
280+
rt_ofw_node_put(bank_np);
281+
282+
if (!bank || args.args_count != 1)
283+
{
284+
lock = rt_err_ptr(-RT_ENOSYS);
285+
}
286+
else
287+
{
288+
lock = hwspinlock_get(bank, bank->base_id + args.args[0]);
289+
}
290+
291+
rt_spin_unlock(&hwspinlock_ops_lock);
292+
293+
return lock;
294+
}
295+
296+
struct rt_hwspinlock *rt_ofw_get_hwspinlock_by_name(struct rt_ofw_node *np, const char *name)
297+
{
298+
int index;
299+
300+
if (!np || !name)
301+
{
302+
return rt_err_ptr(-RT_EINVAL);
303+
}
304+
305+
index = rt_ofw_prop_index_of_string(np, "hwlock-names", name);
306+
307+
if (index < 0)
308+
{
309+
return rt_err_ptr(index);
310+
}
311+
312+
return rt_ofw_get_hwspinlock_by_index(np, index);
313+
}

0 commit comments

Comments
 (0)