Skip to content

Commit f252cb0

Browse files
Lv Zhengrafaeljw
authored andcommitted
ACPI / EC: Add query flushing support
This patch implementes the QR_EC flushing support. Grace periods are implemented from the detection of an SCI_EVT to the submission/completion of the QR_EC transaction. During this period, all EC command transactions are allowed to be submitted. Note that query periods and event periods are intentionally distiguished to allow further improvements. 1. Query period: from the detection of an SCI_EVT to the sumission of the QR_EC command. This period is used for storming prevention, as currently QR_EC is deferred to a work queue rather than directly issued from the IRQ context even there is no other transactions pending, so malicous SCI_EVT GPE can act like "level triggered" to trigger a GPE storm. We need to be prepared for this. And in the future, we may change it to be a part of the advance_transaction() where we will try QR_EC submission in appropriate positions to avoid such GPE storming. 2. Event period: from the detection of an SCI_EVT to the completion of the QR_EC command. We may extend it to the completion of _Qxx evaluation. This is actually a grace period for event flushing, but we only flush queries due to the reason stated in known issue 1. That's also why we use EC_FLAGS_EVENT_xxx. During this period, QR_EC transactions need to pass the flushable submission check. In this patch, the following flags are implemented: 1. EC_FLAGS_EVENT_ENABLED: this is derived from the old EC_FLAGS_QUERY_PENDING flag which can block SCI_EVT handlings. With this flag, the logics implemented by the original flag are extended: 1. Old logic: unless both of the flags are set, the event poller will not be scheduled, and 2. New logic: as soon as both of the flags are set, the evet poller will be scheduled. 2. EC_FLAGS_EVENT_DETECTED: this is also derived from the old EC_FLAGS_QUERY_PENDING flag which can block SCI_EVT detection. It thus can be used to indicate the storming prevention period for query submission. acpi_ec_submit_request()/acpi_ec_complete_request() are invoked to implement this period so that acpi_set_gpe() can be invoked under the "reference count > 0" condition. 3. EC_FLAGS_EVENT_PENDING: this is newly added to indicate the grace period for event flushing (query flushing for now). acpi_ec_submit_request()/acpi_ec_complete_request() are invoked to implement this period so that the flushing process can wait until the event handling (query transaction for now) to be completed. Link: https://bugzilla.kernel.org/show_bug.cgi?id=82611 Link: https://bugzilla.kernel.org/show_bug.cgi?id=77431 Signed-off-by: Lv Zheng <lv.zheng@intel.com> Tested-by: Ortwin Glück <odi@odi.ch> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent e1d4d90 commit f252cb0

1 file changed

Lines changed: 85 additions & 16 deletions

File tree

‎drivers/acpi/ec.c‎

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ enum ec_command {
7676
* when trying to clear the EC */
7777

7878
enum {
79-
EC_FLAGS_QUERY_PENDING, /* Query is pending */
79+
EC_FLAGS_EVENT_ENABLED, /* Event is enabled */
80+
EC_FLAGS_EVENT_PENDING, /* Event is pending */
81+
EC_FLAGS_EVENT_DETECTED, /* Event is detected */
8082
EC_FLAGS_HANDLERS_INSTALLED, /* Handlers for GPE and
8183
* OpReg are installed */
8284
EC_FLAGS_STARTED, /* Driver is started */
@@ -151,6 +153,12 @@ static bool acpi_ec_flushed(struct acpi_ec *ec)
151153
return ec->reference_count == 1;
152154
}
153155

156+
static bool acpi_ec_has_pending_event(struct acpi_ec *ec)
157+
{
158+
return test_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags) ||
159+
test_bit(EC_FLAGS_EVENT_PENDING, &ec->flags);
160+
}
161+
154162
/* --------------------------------------------------------------------------
155163
* EC Registers
156164
* -------------------------------------------------------------------------- */
@@ -318,36 +326,93 @@ static void acpi_ec_clear_storm(struct acpi_ec *ec, u8 flag)
318326
* the flush operation is not in
319327
* progress
320328
* @ec: the EC device
329+
* @allow_event: whether event should be handled
321330
*
322331
* This function must be used before taking a new action that should hold
323332
* the reference count. If this function returns false, then the action
324333
* must be discarded or it will prevent the flush operation from being
325334
* completed.
335+
*
336+
* During flushing, QR_EC command need to pass this check when there is a
337+
* pending event, so that the reference count held for the pending event
338+
* can be decreased by the completion of the QR_EC command.
326339
*/
327-
static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec)
340+
static bool acpi_ec_submit_flushable_request(struct acpi_ec *ec,
341+
bool allow_event)
328342
{
329-
if (!acpi_ec_started(ec))
330-
return false;
343+
if (!acpi_ec_started(ec)) {
344+
if (!allow_event || !acpi_ec_has_pending_event(ec))
345+
return false;
346+
}
331347
acpi_ec_submit_request(ec);
332348
return true;
333349
}
334350

335-
static void acpi_ec_submit_query(struct acpi_ec *ec)
351+
static void acpi_ec_submit_event(struct acpi_ec *ec)
336352
{
337-
if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) {
338-
pr_debug("***** Event started *****\n");
353+
if (!test_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags) ||
354+
!test_bit(EC_FLAGS_EVENT_ENABLED, &ec->flags))
355+
return;
356+
/* Hold reference for pending event */
357+
if (!acpi_ec_submit_flushable_request(ec, true))
358+
return;
359+
if (!test_and_set_bit(EC_FLAGS_EVENT_PENDING, &ec->flags)) {
360+
pr_debug("***** Event query started *****\n");
339361
schedule_work(&ec->work);
362+
return;
363+
}
364+
acpi_ec_complete_request(ec);
365+
}
366+
367+
static void acpi_ec_complete_event(struct acpi_ec *ec)
368+
{
369+
if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
370+
clear_bit(EC_FLAGS_EVENT_PENDING, &ec->flags);
371+
pr_debug("***** Event query stopped *****\n");
372+
/* Unhold reference for pending event */
373+
acpi_ec_complete_request(ec);
374+
/* Check if there is another SCI_EVT detected */
375+
acpi_ec_submit_event(ec);
376+
}
377+
}
378+
379+
static void acpi_ec_submit_detection(struct acpi_ec *ec)
380+
{
381+
/* Hold reference for query submission */
382+
if (!acpi_ec_submit_flushable_request(ec, false))
383+
return;
384+
if (!test_and_set_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags)) {
385+
pr_debug("***** Event detection blocked *****\n");
386+
acpi_ec_submit_event(ec);
387+
return;
340388
}
389+
acpi_ec_complete_request(ec);
341390
}
342391

343-
static void acpi_ec_complete_query(struct acpi_ec *ec)
392+
static void acpi_ec_complete_detection(struct acpi_ec *ec)
344393
{
345394
if (ec->curr->command == ACPI_EC_COMMAND_QUERY) {
346-
clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
347-
pr_debug("***** Event stopped *****\n");
395+
clear_bit(EC_FLAGS_EVENT_DETECTED, &ec->flags);
396+
pr_debug("***** Event detetion unblocked *****\n");
397+
/* Unhold reference for query submission */
398+
acpi_ec_complete_request(ec);
348399
}
349400
}
350401

402+
static void acpi_ec_enable_event(struct acpi_ec *ec)
403+
{
404+
unsigned long flags;
405+
406+
spin_lock_irqsave(&ec->lock, flags);
407+
set_bit(EC_FLAGS_EVENT_ENABLED, &ec->flags);
408+
/*
409+
* An event may be pending even with SCI_EVT=0, so QR_EC should
410+
* always be issued right after started.
411+
*/
412+
acpi_ec_submit_detection(ec);
413+
spin_unlock_irqrestore(&ec->lock, flags);
414+
}
415+
351416
static int ec_transaction_completed(struct acpi_ec *ec)
352417
{
353418
unsigned long flags;
@@ -389,6 +454,7 @@ static void advance_transaction(struct acpi_ec *ec)
389454
t->rdata[t->ri++] = acpi_ec_read_data(ec);
390455
if (t->rlen == t->ri) {
391456
t->flags |= ACPI_EC_COMMAND_COMPLETE;
457+
acpi_ec_complete_event(ec);
392458
if (t->command == ACPI_EC_COMMAND_QUERY)
393459
pr_debug("***** Command(%s) hardware completion *****\n",
394460
acpi_ec_cmd_string(t->command));
@@ -399,6 +465,7 @@ static void advance_transaction(struct acpi_ec *ec)
399465
} else if (t->wlen == t->wi &&
400466
(status & ACPI_EC_FLAG_IBF) == 0) {
401467
t->flags |= ACPI_EC_COMMAND_COMPLETE;
468+
acpi_ec_complete_event(ec);
402469
wakeup = true;
403470
}
404471
goto out;
@@ -407,16 +474,17 @@ static void advance_transaction(struct acpi_ec *ec)
407474
!(status & ACPI_EC_FLAG_SCI) &&
408475
(t->command == ACPI_EC_COMMAND_QUERY)) {
409476
t->flags |= ACPI_EC_COMMAND_POLL;
410-
acpi_ec_complete_query(ec);
477+
acpi_ec_complete_detection(ec);
411478
t->rdata[t->ri++] = 0x00;
412479
t->flags |= ACPI_EC_COMMAND_COMPLETE;
480+
acpi_ec_complete_event(ec);
413481
pr_debug("***** Command(%s) software completion *****\n",
414482
acpi_ec_cmd_string(t->command));
415483
wakeup = true;
416484
} else if ((status & ACPI_EC_FLAG_IBF) == 0) {
417485
acpi_ec_write_cmd(ec, t->command);
418486
t->flags |= ACPI_EC_COMMAND_POLL;
419-
acpi_ec_complete_query(ec);
487+
acpi_ec_complete_detection(ec);
420488
} else
421489
goto err;
422490
goto out;
@@ -437,7 +505,7 @@ static void advance_transaction(struct acpi_ec *ec)
437505
}
438506
out:
439507
if (status & ACPI_EC_FLAG_SCI)
440-
acpi_ec_submit_query(ec);
508+
acpi_ec_submit_detection(ec);
441509
if (wakeup && in_interrupt())
442510
wake_up(&ec->wait);
443511
}
@@ -498,7 +566,7 @@ static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
498566
/* start transaction */
499567
spin_lock_irqsave(&ec->lock, tmp);
500568
/* Enable GPE for command processing (IBF=0/OBF=1) */
501-
if (!acpi_ec_submit_flushable_request(ec)) {
569+
if (!acpi_ec_submit_flushable_request(ec, true)) {
502570
ret = -EINVAL;
503571
goto unlock;
504572
}
@@ -879,7 +947,9 @@ static void acpi_ec_gpe_poller(struct work_struct *work)
879947
{
880948
struct acpi_ec *ec = container_of(work, struct acpi_ec, work);
881949

950+
pr_debug("***** Event poller started *****\n");
882951
acpi_ec_query(ec, NULL);
952+
pr_debug("***** Event poller stopped *****\n");
883953
}
884954

885955
static u32 acpi_ec_gpe_handler(acpi_handle gpe_device,
@@ -949,7 +1019,6 @@ static struct acpi_ec *make_acpi_ec(void)
9491019

9501020
if (!ec)
9511021
return NULL;
952-
ec->flags = 1 << EC_FLAGS_QUERY_PENDING;
9531022
mutex_init(&ec->mutex);
9541023
init_waitqueue_head(&ec->wait);
9551024
INIT_LIST_HEAD(&ec->list);
@@ -1100,7 +1169,7 @@ static int acpi_ec_add(struct acpi_device *device)
11001169
ret = ec_install_handlers(ec);
11011170

11021171
/* EC is fully operational, allow queries */
1103-
clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
1172+
acpi_ec_enable_event(ec);
11041173

11051174
/* Clear stale _Q events if hardware might require that */
11061175
if (EC_FLAGS_CLEAR_ON_RESUME)

0 commit comments

Comments
 (0)