summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/uacpi/mutex.c
blob: 44cbac3211ce123d5f2e293a65e0ec50167b0003 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <uacpi/platform/atomic.h>
#include <uacpi/internal/mutex.h>
#include <uacpi/internal/log.h>
#include <uacpi/internal/registers.h>
#include <uacpi/internal/context.h>
#include <uacpi/kernel_api.h>
#include <uacpi/internal/namespace.h>

#ifndef UACPI_BAREBONES_MODE

#ifndef UACPI_REDUCED_HARDWARE

#define GLOBAL_LOCK_PENDING (1 << 0)

#define GLOBAL_LOCK_OWNED_BIT 1
#define GLOBAL_LOCK_OWNED (1 << GLOBAL_LOCK_OWNED_BIT)

#define GLOBAL_LOCK_MASK 3u

static uacpi_bool try_acquire_global_lock_from_firmware(uacpi_u32 *lock)
{
    uacpi_u32 value, new_value;
    uacpi_bool was_owned;

    value = *(volatile uacpi_u32*)lock;
    do {
        was_owned = (value & GLOBAL_LOCK_OWNED) >> GLOBAL_LOCK_OWNED_BIT;

        // Clear both owned & pending bits.
        new_value = value & ~GLOBAL_LOCK_MASK;

        // Set owned unconditionally
        new_value |= GLOBAL_LOCK_OWNED;

        // Set pending iff the lock was owned at the time of reading
        if (was_owned)
            new_value |= GLOBAL_LOCK_PENDING;
    } while (!uacpi_atomic_cmpxchg32(lock, &value, new_value));

    return !was_owned;
}

static uacpi_bool do_release_global_lock_to_firmware(uacpi_u32 *lock)
{
    uacpi_u32 value, new_value;

    value = *(volatile uacpi_u32*)lock;
    do {
        new_value = value & ~GLOBAL_LOCK_MASK;
    } while (!uacpi_atomic_cmpxchg32(lock, &value, new_value));

    return value & GLOBAL_LOCK_PENDING;
}

static uacpi_status uacpi_acquire_global_lock_from_firmware(void)
{
    uacpi_cpu_flags flags;
    uacpi_u16 spins = 0;
    uacpi_bool success;

    if (!g_uacpi_rt_ctx.has_global_lock)
        return UACPI_STATUS_OK;

    flags = uacpi_kernel_lock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock);
    for (;;) {
        spins++;
        uacpi_trace(
            "trying to acquire the global lock from firmware... (attempt %u)\n",
            spins
        );

        success = try_acquire_global_lock_from_firmware(
            &g_uacpi_rt_ctx.facs->global_lock
        );
        if (success)
            break;

        if (uacpi_unlikely(spins == 0xFFFF))
            break;

        g_uacpi_rt_ctx.global_lock_pending = UACPI_TRUE;
        uacpi_trace(
            "global lock is owned by firmware, waiting for a release "
            "notification...\n"
        );
        uacpi_kernel_unlock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock, flags);

        uacpi_kernel_wait_for_event(g_uacpi_rt_ctx.global_lock_event, 0xFFFF);
        flags = uacpi_kernel_lock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock);
    }

    g_uacpi_rt_ctx.global_lock_pending = UACPI_FALSE;
    uacpi_kernel_unlock_spinlock(g_uacpi_rt_ctx.global_lock_spinlock, flags);

    if (uacpi_unlikely(!success)) {
        uacpi_error("unable to acquire global lock after %u attempts\n", spins);
        return UACPI_STATUS_HARDWARE_TIMEOUT;
    }

    uacpi_trace("global lock successfully acquired after %u attempt%s\n",
                spins, spins > 1 ? "s" : "");
    return UACPI_STATUS_OK;
}

static void uacpi_release_global_lock_to_firmware(void)
{
    if (!g_uacpi_rt_ctx.has_global_lock)
        return;

    uacpi_trace("releasing the global lock to firmware...\n");
    if (do_release_global_lock_to_firmware(&g_uacpi_rt_ctx.facs->global_lock)) {
        uacpi_trace("notifying firmware of the global lock release since the "
                    "pending bit was set\n");
        uacpi_write_register_field(UACPI_REGISTER_FIELD_GBL_RLS, 1);
    }
}
#endif

UACPI_ALWAYS_OK_FOR_REDUCED_HARDWARE(
    uacpi_status uacpi_acquire_global_lock_from_firmware(void)
)
UACPI_STUB_IF_REDUCED_HARDWARE(
    void uacpi_release_global_lock_to_firmware(void)
)

uacpi_status uacpi_acquire_native_mutex_with_timeout(
    uacpi_handle mtx, uacpi_u16 timeout
)
{
    uacpi_status ret;

    if (uacpi_unlikely(mtx == UACPI_NULL))
        return UACPI_STATUS_INVALID_ARGUMENT;

    ret = uacpi_kernel_acquire_mutex(mtx, timeout);
    if (uacpi_likely_success(ret))
        return ret;

    if (uacpi_unlikely(ret != UACPI_STATUS_TIMEOUT || timeout == 0xFFFF)) {
        uacpi_error(
            "unexpected status %08X (%s) while acquiring %p (timeout=%04X)\n",
            ret, uacpi_status_to_string(ret), mtx, timeout
        );
    }

    return ret;
}

uacpi_status uacpi_acquire_global_lock(uacpi_u16 timeout, uacpi_u32 *out_seq)
{
    uacpi_status ret;

    UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED);

    if (uacpi_unlikely(out_seq == UACPI_NULL))
        return UACPI_STATUS_INVALID_ARGUMENT;

    ret = uacpi_acquire_native_mutex_with_timeout(
        g_uacpi_rt_ctx.global_lock_mutex->handle, timeout
    );
    if (ret != UACPI_STATUS_OK)
        return ret;

    ret = uacpi_acquire_global_lock_from_firmware();
    if (uacpi_unlikely_error(ret)) {
        uacpi_release_native_mutex(g_uacpi_rt_ctx.global_lock_mutex->handle);
        return ret;
    }

    if (uacpi_unlikely(g_uacpi_rt_ctx.global_lock_seq_num == 0xFFFFFFFF))
        g_uacpi_rt_ctx.global_lock_seq_num = 0;

    *out_seq = g_uacpi_rt_ctx.global_lock_seq_num++;
    g_uacpi_rt_ctx.global_lock_acquired = UACPI_TRUE;
    return UACPI_STATUS_OK;
}

uacpi_status uacpi_release_global_lock(uacpi_u32 seq)
{
    UACPI_ENSURE_INIT_LEVEL_AT_LEAST(UACPI_INIT_LEVEL_SUBSYSTEM_INITIALIZED);

    if (uacpi_unlikely(!g_uacpi_rt_ctx.global_lock_acquired ||
                       seq != g_uacpi_rt_ctx.global_lock_seq_num))
        return UACPI_STATUS_INVALID_ARGUMENT;

    g_uacpi_rt_ctx.global_lock_acquired = UACPI_FALSE;
    uacpi_release_global_lock_to_firmware();
    uacpi_release_native_mutex(g_uacpi_rt_ctx.global_lock_mutex->handle);

    return UACPI_STATUS_OK;
}

uacpi_bool uacpi_this_thread_owns_aml_mutex(uacpi_mutex *mutex)
{
    uacpi_thread_id id;

    id = UACPI_ATOMIC_LOAD_THREAD_ID(&mutex->owner);
    return id == uacpi_kernel_get_thread_id();
}

uacpi_status uacpi_acquire_aml_mutex(uacpi_mutex *mutex, uacpi_u16 timeout)
{
    uacpi_thread_id this_id;
    uacpi_status ret = UACPI_STATUS_OK;

    this_id = uacpi_kernel_get_thread_id();
    if (UACPI_ATOMIC_LOAD_THREAD_ID(&mutex->owner) == this_id) {
        if (uacpi_unlikely(mutex->depth == 0xFFFF)) {
            uacpi_warn(
                "failing an attempt to acquire mutex @%p, too many recursive "
                "acquires\n", mutex
            );
            return UACPI_STATUS_DENIED;
        }

        mutex->depth++;
        return ret;
    }

    uacpi_namespace_write_unlock();
    ret = uacpi_acquire_native_mutex_with_timeout(mutex->handle, timeout);
    if (ret != UACPI_STATUS_OK)
        goto out;

    if (mutex->handle == g_uacpi_rt_ctx.global_lock_mutex->handle) {
        ret = uacpi_acquire_global_lock_from_firmware();
        if (uacpi_unlikely_error(ret)) {
            uacpi_release_native_mutex(mutex->handle);
            goto out;
        }
    }

    UACPI_ATOMIC_STORE_THREAD_ID(&mutex->owner, this_id);
    mutex->depth = 1;

out:
    uacpi_namespace_write_lock();
    return ret;
}

uacpi_status uacpi_release_aml_mutex(uacpi_mutex *mutex)
{
    if (mutex->depth-- > 1)
        return UACPI_STATUS_OK;

    if (mutex->handle == g_uacpi_rt_ctx.global_lock_mutex->handle)
        uacpi_release_global_lock_to_firmware();

    UACPI_ATOMIC_STORE_THREAD_ID(&mutex->owner, UACPI_THREAD_ID_NONE);
    uacpi_release_native_mutex(mutex->handle);

    return UACPI_STATUS_OK;
}

uacpi_status uacpi_recursive_lock_init(struct uacpi_recursive_lock *lock)
{
    lock->mutex = uacpi_kernel_create_mutex();
    if (uacpi_unlikely(lock->mutex == UACPI_NULL))
        return UACPI_STATUS_OUT_OF_MEMORY;

    lock->owner = UACPI_THREAD_ID_NONE;
    lock->depth = 0;

    return UACPI_STATUS_OK;
}

uacpi_status uacpi_recursive_lock_deinit(struct uacpi_recursive_lock *lock)
{
    if (uacpi_unlikely(lock->depth)) {
        uacpi_warn(
            "de-initializing active recursive lock %p with depth=%zu\n",
            lock, lock->depth
        );
        lock->depth = 0;
    }

    lock->owner = UACPI_THREAD_ID_NONE;

    if (lock->mutex != UACPI_NULL) {
        uacpi_kernel_free_mutex(lock->mutex);
        lock->mutex = UACPI_NULL;
    }

    return UACPI_STATUS_OK;
}

uacpi_status uacpi_recursive_lock_acquire(struct uacpi_recursive_lock *lock)
{
    uacpi_thread_id this_id;
    uacpi_status ret = UACPI_STATUS_OK;

    this_id = uacpi_kernel_get_thread_id();
    if (UACPI_ATOMIC_LOAD_THREAD_ID(&lock->owner) == this_id) {
        lock->depth++;
        return ret;
    }

    ret = uacpi_acquire_native_mutex(lock->mutex);
    if (uacpi_unlikely_error(ret))
        return ret;

    UACPI_ATOMIC_STORE_THREAD_ID(&lock->owner, this_id);
    lock->depth = 1;
    return ret;
}

uacpi_status uacpi_recursive_lock_release(struct uacpi_recursive_lock *lock)
{
    if (lock->depth-- > 1)
        return UACPI_STATUS_OK;

    UACPI_ATOMIC_STORE_THREAD_ID(&lock->owner, UACPI_THREAD_ID_NONE);
    return uacpi_release_native_mutex(lock->mutex);
}

uacpi_status uacpi_rw_lock_init(struct uacpi_rw_lock *lock)
{
    lock->read_mutex = uacpi_kernel_create_mutex();
    if (uacpi_unlikely(lock->read_mutex == UACPI_NULL))
        return UACPI_STATUS_OUT_OF_MEMORY;

    lock->write_mutex = uacpi_kernel_create_mutex();
    if (uacpi_unlikely(lock->write_mutex == UACPI_NULL)) {
        uacpi_kernel_free_mutex(lock->read_mutex);
        lock->read_mutex = UACPI_NULL;
        return UACPI_STATUS_OUT_OF_MEMORY;
    }

    lock->num_readers = 0;
    return UACPI_STATUS_OK;
}

uacpi_status uacpi_rw_lock_deinit(struct uacpi_rw_lock *lock)
{
    if (uacpi_unlikely(lock->num_readers)) {
        uacpi_warn("de-initializing rw_lock %p with %zu active readers\n",
                   lock, lock->num_readers);
        lock->num_readers = 0;
    }

    if (lock->read_mutex != UACPI_NULL) {
        uacpi_kernel_free_mutex(lock->read_mutex);
        lock->read_mutex = UACPI_NULL;
    }
    if (lock->write_mutex != UACPI_NULL) {
        uacpi_kernel_free_mutex(lock->write_mutex);
        lock->write_mutex = UACPI_NULL;
    }

    return UACPI_STATUS_OK;
}

uacpi_status uacpi_rw_lock_read(struct uacpi_rw_lock *lock)
{
    uacpi_status ret;

    ret = uacpi_acquire_native_mutex(lock->read_mutex);
    if (uacpi_unlikely_error(ret))
        return ret;

    if (lock->num_readers++ == 0) {
        ret = uacpi_acquire_native_mutex(lock->write_mutex);
        if (uacpi_unlikely_error(ret))
            lock->num_readers = 0;
    }

    uacpi_kernel_release_mutex(lock->read_mutex);
    return ret;
}

uacpi_status uacpi_rw_unlock_read(struct uacpi_rw_lock *lock)
{
    uacpi_status ret;

    ret = uacpi_acquire_native_mutex(lock->read_mutex);
    if (uacpi_unlikely_error(ret))
        return ret;

    if (lock->num_readers-- == 1)
        uacpi_release_native_mutex(lock->write_mutex);

    uacpi_kernel_release_mutex(lock->read_mutex);
    return ret;
}

uacpi_status uacpi_rw_lock_write(struct uacpi_rw_lock *lock)
{
    return uacpi_acquire_native_mutex(lock->write_mutex);
}

uacpi_status uacpi_rw_unlock_write(struct uacpi_rw_lock *lock)
{
    return uacpi_release_native_mutex(lock->write_mutex);
}

#endif // !UACPI_BAREBONES_MODE