-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathps.c
More file actions
547 lines (437 loc) · 13.8 KB
/
ps.c
File metadata and controls
547 lines (437 loc) · 13.8 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
* Copyright 2017-2023 Morse Micro
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include <linux/types.h>
#include <linux/atomic.h>
#include <linux/slab.h>
#include <linux/atomic.h>
#include <linux/workqueue.h>
#include <linux/completion.h>
#include <linux/gpio.h>
#include <linux/jiffies.h>
#include "morse.h"
#include "debug.h"
#include "morse_commands.h"
#include "skbq.h"
#include "mac.h"
#include "bus.h"
#include "hw.h"
#include "ps.h"
#include "trace.h"
#include "dot11ah/s1g_ieee80211.h"
#define MORSE_PS_DBG(_m, _f, _a...) morse_dbg(FEATURE_ID_POWERSAVE, _m, _f, ##_a)
#define MORSE_PS_WARN(_m, _f, _a...) morse_warn(FEATURE_ID_POWERSAVE, _m, _f, ##_a)
static bool morse_ps_is_busy_pin_asserted(struct morse *mors)
{
bool active_high = !(mors->firmware_flags & MORSE_FW_FLAGS_BUSY_ACTIVE_LOW);
if (!mors->cfg->mm_ps_gpios_supported)
return false;
return (!!gpio_get_value(mors->cfg->mm_ps_async_gpio) == active_high);
}
static u8 morse_ps_get_warm_boot_time_ms(struct morse *mors)
{
return mors->cfg->get_warm_boot_time_ms(mors->chip_id);
}
static void morse_ps_set_wake_gpio(struct morse *mors, bool raise)
{
int ret;
if (!mors->cfg->mm_ps_gpios_supported)
return;
ret = gpio_direction_output(mors->cfg->mm_wake_gpio, (raise) ? 1 : 0);
if (ret) {
MORSE_ERR(mors, "%s: Failed to %s wake pin (ret:%d)", __func__,
(raise) ? "raise" : "lower", ret);
return;
}
trace_ps_wake_gpio(raise);
MORSE_PS_DBG(mors, "%s: %s wake up pin", __func__, (raise) ? "set" : "cleared");
}
static void morse_ps_wait_after_wake_pin_raise(struct morse *mors)
{
unsigned long rem;
unsigned long timeout;
unsigned int max_boot_time_ms;
unsigned int warm_boot_time_ms = morse_ps_get_warm_boot_time_ms(mors);
bool hw_signals_wake = !!(mors->firmware_flags &
MORSE_FW_FLAGS_TOGGLES_BUSY_PIN_ON_WAKE_PIN);
if (!mors->cfg->mm_ps_gpios_supported)
return;
if (morse_ps_is_busy_pin_asserted(mors))
return; /* device already indicating busy - don't wait the delay */
/* Wake time includes time for driver to warm boot (multiplied to give some buffer) and the
* theoretical max time that the chip could be in RX
*/
if (hw_signals_wake) {
max_boot_time_ms = 2 * warm_boot_time_ms +
MORSE_USECS_TO_MSECS_CEIL(S1G_MAX_PACKET_AIR_TIME_USECS);
} else {
max_boot_time_ms = warm_boot_time_ms;
}
timeout = msecs_to_jiffies(max_boot_time_ms);
rem = wait_for_completion_timeout(mors->ps.awake, timeout);
MORSE_PS_DBG(mors, "%s: took %dms to wake\n", __func__, jiffies_to_msecs(timeout - rem));
if (hw_signals_wake && rem > 0 && jiffies_to_msecs(rem) <= warm_boot_time_ms) {
trace_ps_chip_wake_error(warm_boot_time_ms);
MORSE_PS_WARN(mors, "%s: HW took longer than expected to signal wake, continuing\n",
__func__);
}
/* Warn if HW did not signal within the expected boot period - continue anyway */
if (hw_signals_wake && rem == 0) {
trace_ps_chip_wake_error(timeout);
MORSE_PS_WARN(mors, "%s: HW did not signal wake within expected boot period\n",
__func__);
}
}
static int morse_ps_wakeup(struct morse_ps *mps)
{
DECLARE_COMPLETION_ONSTACK(awake);
struct morse *mors = container_of(mps, struct morse, ps);
if (!mps->is_drv_ps_allowed)
return 0;
if (!mps->suspended)
return 0;
trace_ps_wake_start(0);
WRITE_ONCE(mors->ps.awake, &awake);
morse_ps_set_wake_gpio(mors, true);
morse_ps_wait_after_wake_pin_raise(mors);
WRITE_ONCE(mors->ps.awake, NULL);
trace_ps_wake_end(0);
morse_set_bus_enable(mors, true);
mps->suspended = false;
return 0;
}
static int morse_ps_sleep(struct morse_ps *mps)
{
struct morse *mors = container_of(mps, struct morse, ps);
if (!mps->is_drv_ps_allowed)
return 0;
if (mps->suspended)
return 0;
trace_ps_sleep(0);
mps->suspended = true;
morse_set_bus_enable(mors, false);
morse_ps_set_wake_gpio(mors, false);
return 0;
}
static irqreturn_t morse_ps_irq_handle(int irq, void *arg)
{
struct morse_ps *mps = (struct morse_ps *)arg;
struct morse *mors = container_of(mps, struct morse, ps);
struct completion *awake = READ_ONCE(mps->awake);
if (awake)
complete(awake);
else
queue_work(mors->chip_wq, &mps->async_wake_work);
trace_ps_to_host_busy_irq((awake) ? 1 : 0);
return IRQ_HANDLED;
}
static void morse_ps_async_wake_work(struct work_struct *work)
{
struct morse_ps *mps = container_of(work, struct morse_ps, async_wake_work);
struct morse *mors = container_of(mps, struct morse, ps);
if (test_bit(MORSE_STATE_FLAG_SYSTEM_IN_SUSPEND, &mors->state_flags))
return;
mutex_lock(&mps->lock);
morse_ps_wakeup(mps);
mutex_unlock(&mps->lock);
}
void morse_ps_bus_activity(struct morse *mors, int timeout_ms)
{
mutex_lock(&mors->ps.lock);
mors->ps.bus_ps_timeout = jiffies + msecs_to_jiffies(timeout_ms);
mutex_unlock(&mors->ps.lock);
}
static bool morse_ps_is_interface_enabled_nolock(struct morse *mors)
{
struct morse_ps *mps = &mors->ps;
lockdep_assert_held(&mps->lock);
return mps->mors_vif && mps->mors_vif->is_ps_enabled;
}
bool morse_ps_is_interface_enabled(struct morse *mors)
{
bool enabled;
struct morse_ps *mps = &mors->ps;
mutex_lock(&mps->lock);
enabled = morse_ps_is_interface_enabled_nolock(mors);
mutex_unlock(&mps->lock);
return enabled;
}
static bool is_interface_same_nolock(struct morse *mors, struct morse_vif *mors_vif)
{
lockdep_assert_held(&mors->ps.lock);
return mors->ps.mors_vif == mors_vif;
}
bool morse_ps_is_interface_same(struct morse *mors, struct morse_vif *mors_vif)
{
struct morse_ps *mps = &mors->ps;
bool ifaces_are_same;
mutex_lock(&mps->lock);
ifaces_are_same = is_interface_same_nolock(mors, mors_vif);
mutex_unlock(&mps->lock);
return ifaces_are_same;
}
static int morse_ps_evaluate(struct morse_ps *mps)
{
struct morse *mors = container_of(mps, struct morse, ps);
bool needs_wake = false;
bool eval_later = false;
unsigned long flags_on_entry = READ_ONCE(mors->chip_if->event_flags);
flags_on_entry &= ~BIT(MORSE_DATA_TRAFFIC_PAUSE_PEND);
needs_wake = (mps->wakers > 0);
needs_wake |= (morse_ps_is_interface_enabled_nolock(mors) == false);
needs_wake |= (flags_on_entry > 0);
needs_wake |= (mors->cfg->ops->skbq_get_tx_buffered_count(mors) > 0);
if (!needs_wake &&
mps->dynamic_ps_en &&
morse_is_data_tx_allowed(mors) && time_before(jiffies, mps->bus_ps_timeout)) {
/*
* Eval later if there is nothing explicitly holding the bus awake,
* but the bus ps timeout has been set to some time in the future
* (i.e. network traffic has recently occurred).
*
* In TWT, the device may go into TWT sleep immediately without
* caring about recent network traffic.
*/
needs_wake = true;
eval_later = true;
}
if (needs_wake) {
morse_ps_wakeup(mps);
} else if (morse_ps_is_busy_pin_asserted(mors)) {
/* Chip has something to send across the bus, re-evaluate later */
eval_later = true;
} else {
MORSE_WARN_ON(FEATURE_ID_POWERSAVE, eval_later);
morse_ps_sleep(mps);
}
if (eval_later) {
static const int default_bus_timeout_ms = 5;
unsigned long expire = jiffies + msecs_to_jiffies(default_bus_timeout_ms);
if (mps->dynamic_ps_en && time_after(mps->bus_ps_timeout, expire))
expire = mps->bus_ps_timeout;
expire = expire - jiffies;
MORSE_PS_DBG(mors, "%s: Delaying eval work by %d ms\n",
__func__, jiffies_to_msecs(expire));
cancel_delayed_work(&mps->delayed_eval_work);
queue_delayed_work(mors->chip_wq, &mps->delayed_eval_work, expire);
}
return 0;
}
void morse_ps_force_eval(struct morse *mors)
{
if (!mors->ps.is_drv_ps_allowed)
return;
mutex_lock(&mors->ps.lock);
morse_ps_evaluate(&mors->ps);
mutex_unlock(&mors->ps.lock);
}
void morse_ps_queue_eval(struct morse *mors)
{
if (!mors->ps.is_drv_ps_allowed)
return;
queue_delayed_work(mors->chip_wq, &mors->ps.delayed_eval_work, 0);
}
static void morse_ps_evaluate_work(struct work_struct *work)
{
struct morse_ps *mps = container_of(work, struct morse_ps, delayed_eval_work.work);
struct morse *mors = container_of(mps, struct morse, ps);
if (test_bit(MORSE_STATE_FLAG_SYSTEM_IN_SUSPEND, &mors->state_flags))
return;
morse_ps_force_eval(mors);
}
int morse_ps_wakers_inc(struct morse *mors)
{
struct morse_ps *mps = &mors->ps;
if (!mps->is_drv_ps_allowed)
return 0;
mutex_lock(&mps->lock);
mps->wakers++;
mutex_unlock(&mps->lock);
return 0;
}
int morse_ps_wakers_dec(struct morse *mors)
{
int ret;
struct morse_ps *mps = &mors->ps;
if (!mps->is_drv_ps_allowed)
return 0;
mutex_lock(&mps->lock);
if (mps->wakers == 0) {
MORSE_PS_WARN(mors, "%s: number of wakers = 0\n", __func__);
ret = -EFAULT;
} else {
mps->wakers--;
ret = 0;
}
mutex_unlock(&mps->lock);
return ret;
}
static void update_interface_state_nolock(struct morse *mors, struct morse_vif *mors_vif,
bool enabled)
{
struct morse_ps *mps = &mors->ps;
mps->mors_vif = mors_vif;
if (mps->mors_vif)
mps->mors_vif->is_ps_enabled = enabled;
}
void morse_ps_update_interface_state(struct morse *mors, struct morse_vif *mors_vif, bool enabled)
{
struct morse_ps *mps = &mors->ps;
if (!mps->is_drv_ps_allowed)
return;
mutex_lock(&mps->lock);
update_interface_state_nolock(mors, mors_vif, enabled);
mutex_unlock(&mps->lock);
morse_ps_queue_eval(mors);
}
void morse_ps_iface_down_notify(struct morse *mors, struct morse_vif *mors_vif)
{
struct morse_ps *mps = &mors->ps;
bool updated = false;
mutex_lock(&mps->lock);
if (is_interface_same_nolock(mors, mors_vif)) {
update_interface_state_nolock(mors, NULL, false);
updated = true;
}
mutex_unlock(&mps->lock);
if (updated)
morse_ps_force_eval(mors);
}
int mors_ps_get_net_timeout_ms(struct morse *mors)
{
int timeout_ms;
struct morse_ps *mps = &mors->ps;
static const int default_uapsd_ps_net_timeout_ms = 5;
mutex_lock(&mps->lock);
timeout_ms = mps->ps_net_timeout_ms;
if (mors->uapsd_per_ac) /* U-APSD will override default setting when active */
timeout_ms = min(timeout_ms, default_uapsd_ps_net_timeout_ms);
mutex_unlock(&mps->lock);
return timeout_ms;
}
void mors_ps_set_net_timeout_ms(struct morse *mors, int timeout_ms)
{
struct morse_ps *mps = &mors->ps;
mutex_lock(&mps->lock);
mps->ps_net_timeout_ms = timeout_ms;
mutex_unlock(&mps->lock);
}
int morse_ps_system_suspend(struct morse *mors)
{
int ret;
int irq;
struct morse_ps *mps = &mors->ps;
MORSE_WARN_ON(FEATURE_ID_POWERSAVE, !mors->cfg->mm_ps_gpios_supported);
/* Disable bus interrupts before entering suspend. This will prevent
* misfires upon wake.
*/
morse_bus_set_irq(mors, false);
/* Decrement waker set in calling site */
morse_ps_wakers_dec(mors);
irq = gpio_to_irq(mors->cfg->mm_ps_async_gpio);
if (irq < 0) {
ret = -ENOTSUPP;
goto exit;
}
/* Disable the async interrupt and cancel any pending work */
disable_irq(irq);
cancel_work_sync(&mps->async_wake_work);
cancel_delayed_work_sync(&mps->delayed_eval_work);
/* Make the async wake gpio the system wake-from-suspend IRQ */
ret = enable_irq_wake(irq);
if (ret) {
/* The system will not suspend, restore the irq */
enable_irq(irq);
goto exit;
}
mutex_lock(&mps->lock);
if (mps->wakers)
MORSE_PS_WARN(mors, "%s: wakers in suspend:%d\n", __func__, mps->wakers);
morse_ps_sleep(mps);
MORSE_WARN_ON(FEATURE_ID_POWERSAVE, !mps->suspended);
mutex_unlock(&mps->lock);
exit:
if (ret)
MORSE_ERR(mors, "%s: failed to enable suspend wake irq (ret:%d)\n", __func__, ret);
else
MORSE_INFO(mors, "%s: complete\n", __func__);
return ret;
}
void morse_ps_system_resume(struct morse *mors)
{
int irq;
struct morse_ps *mps = &mors->ps;
mutex_lock(&mps->lock);
irq = gpio_to_irq(mors->cfg->mm_ps_async_gpio);
if (irq >= 0) {
disable_irq_wake(irq);
enable_irq(irq);
} else {
MORSE_PS_WARN(mors, "%s: invalid irq for system resume\n", __func__);
}
morse_ps_wakeup(mps);
mutex_unlock(&mps->lock);
morse_bus_set_irq(mors, true);
MORSE_INFO(mors, "%s: complete\n", __func__);
}
int morse_ps_init(struct morse *mors, bool enable, bool enable_dynamic_ps)
{
int ret;
int irq = gpio_to_irq(mors->cfg->mm_ps_async_gpio);
struct morse_ps *mps = &mors->ps;
static const int default_ps_net_timeout_ms = 90;
mps->ps_net_timeout_ms = default_ps_net_timeout_ms;
mps->is_drv_ps_allowed = enable;
mps->bus_ps_timeout = jiffies;
mps->dynamic_ps_en = enable_dynamic_ps;
mps->suspended = false;
mps->wakers = 0;
mutex_init(&mps->lock);
if (mps->is_drv_ps_allowed) {
INIT_WORK(&mps->async_wake_work, morse_ps_async_wake_work);
INIT_DELAYED_WORK(&mps->delayed_eval_work, morse_ps_evaluate_work);
morse_ps_update_interface_state(mors, NULL, false);
if (!mors->cfg->mm_ps_gpios_supported) {
/* The rest of the code is GPIO related, we need to bail */
return 0;
}
/**
* SW-1674: Should be the following, but issues observed.
* gpio_request_one(mors->cfg->mm_wake_gpio, GPIOF_OPEN_DRAIN, NULL);
*/
/* Default to allow chip to wakeup */
ret = gpio_request(mors->cfg->mm_wake_gpio, "morse-wakeup-ctrl");
if (ret < 0) {
MORSE_PR_ERR(FEATURE_ID_POWERSAVE, "Failed to acquire wakeup gpio.\n");
return ret;
}
gpio_direction_output(mors->cfg->mm_wake_gpio, 1);
gpio_request(mors->cfg->mm_ps_async_gpio, "morse-async-wakeup-ctrl");
/* The following input gpio must be configured with pull-down */
gpio_direction_input(mors->cfg->mm_ps_async_gpio);
ret = request_irq(irq, (irq_handler_t)morse_ps_irq_handle,
(mors->firmware_flags & MORSE_FW_FLAGS_BUSY_ACTIVE_LOW) ?
IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING,
"async_wakeup_from_chip", mps);
MORSE_WARN_ON(FEATURE_ID_POWERSAVE, ret);
}
return 0;
}
void morse_ps_finish(struct morse *mors)
{
struct morse_ps *mps = &mors->ps;
if (mps->is_drv_ps_allowed) {
mps->is_drv_ps_allowed = false;
mps->dynamic_ps_en = false;
if (mors->cfg->mm_ps_gpios_supported) {
free_irq(gpio_to_irq(mors->cfg->mm_ps_async_gpio), mps);
gpio_free(mors->cfg->mm_ps_async_gpio);
gpio_free(mors->cfg->mm_wake_gpio);
}
cancel_work_sync(&mps->async_wake_work);
cancel_delayed_work_sync(&mps->delayed_eval_work);
}
}