aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/acpi/ec.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/acpi/ec.c')
-rw-r--r--drivers/acpi/ec.c98
1 files changed, 66 insertions, 32 deletions
diff --git a/drivers/acpi/ec.c b/drivers/acpi/ec.c
index ef42316f89f5..30f3ef236ecb 100644
--- a/drivers/acpi/ec.c
+++ b/drivers/acpi/ec.c
@@ -70,7 +70,7 @@ enum ec_command {
70#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ 70#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
71#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */ 71#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
72 72
73#define ACPI_EC_STORM_THRESHOLD 20 /* number of false interrupts 73#define ACPI_EC_STORM_THRESHOLD 8 /* number of false interrupts
74 per one transaction */ 74 per one transaction */
75 75
76enum { 76enum {
@@ -100,8 +100,11 @@ struct transaction {
100 u8 *rdata; 100 u8 *rdata;
101 unsigned short irq_count; 101 unsigned short irq_count;
102 u8 command; 102 u8 command;
103 u8 wi;
104 u8 ri;
103 u8 wlen; 105 u8 wlen;
104 u8 rlen; 106 u8 rlen;
107 bool done;
105}; 108};
106 109
107static struct acpi_ec { 110static struct acpi_ec {
@@ -178,34 +181,46 @@ static int ec_transaction_done(struct acpi_ec *ec)
178 unsigned long flags; 181 unsigned long flags;
179 int ret = 0; 182 int ret = 0;
180 spin_lock_irqsave(&ec->curr_lock, flags); 183 spin_lock_irqsave(&ec->curr_lock, flags);
181 if (!ec->curr || (!ec->curr->wlen && !ec->curr->rlen)) 184 if (!ec->curr || ec->curr->done)
182 ret = 1; 185 ret = 1;
183 spin_unlock_irqrestore(&ec->curr_lock, flags); 186 spin_unlock_irqrestore(&ec->curr_lock, flags);
184 return ret; 187 return ret;
185} 188}
186 189
190static void start_transaction(struct acpi_ec *ec)
191{
192 ec->curr->irq_count = ec->curr->wi = ec->curr->ri = 0;
193 ec->curr->done = false;
194 acpi_ec_write_cmd(ec, ec->curr->command);
195}
196
187static void gpe_transaction(struct acpi_ec *ec, u8 status) 197static void gpe_transaction(struct acpi_ec *ec, u8 status)
188{ 198{
189 unsigned long flags; 199 unsigned long flags;
190 spin_lock_irqsave(&ec->curr_lock, flags); 200 spin_lock_irqsave(&ec->curr_lock, flags);
191 if (!ec->curr) 201 if (!ec->curr)
192 goto unlock; 202 goto unlock;
193 if (ec->curr->wlen > 0) { 203 if (ec->curr->wlen > ec->curr->wi) {
194 if ((status & ACPI_EC_FLAG_IBF) == 0) { 204 if ((status & ACPI_EC_FLAG_IBF) == 0)
195 acpi_ec_write_data(ec, *(ec->curr->wdata++)); 205 acpi_ec_write_data(ec,
196 --ec->curr->wlen; 206 ec->curr->wdata[ec->curr->wi++]);
197 } else 207 else
198 /* false interrupt, state didn't change */ 208 goto err;
199 ++ec->curr->irq_count; 209 } else if (ec->curr->rlen > ec->curr->ri) {
200
201 } else if (ec->curr->rlen > 0) {
202 if ((status & ACPI_EC_FLAG_OBF) == 1) { 210 if ((status & ACPI_EC_FLAG_OBF) == 1) {
203 *(ec->curr->rdata++) = acpi_ec_read_data(ec); 211 ec->curr->rdata[ec->curr->ri++] = acpi_ec_read_data(ec);
204 --ec->curr->rlen; 212 if (ec->curr->rlen == ec->curr->ri)
213 ec->curr->done = true;
205 } else 214 } else
206 /* false interrupt, state didn't change */ 215 goto err;
207 ++ec->curr->irq_count; 216 } else if (ec->curr->wlen == ec->curr->wi &&
208 } 217 (status & ACPI_EC_FLAG_IBF) == 0)
218 ec->curr->done = true;
219 goto unlock;
220err:
221 /* false interrupt, state didn't change */
222 if (in_interrupt())
223 ++ec->curr->irq_count;
209unlock: 224unlock:
210 spin_unlock_irqrestore(&ec->curr_lock, flags); 225 spin_unlock_irqrestore(&ec->curr_lock, flags);
211} 226}
@@ -215,6 +230,15 @@ static int acpi_ec_wait(struct acpi_ec *ec)
215 if (wait_event_timeout(ec->wait, ec_transaction_done(ec), 230 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
216 msecs_to_jiffies(ACPI_EC_DELAY))) 231 msecs_to_jiffies(ACPI_EC_DELAY)))
217 return 0; 232 return 0;
233 /* try restart command if we get any false interrupts */
234 if (ec->curr->irq_count &&
235 (acpi_ec_read_status(ec) & ACPI_EC_FLAG_IBF) == 0) {
236 pr_debug(PREFIX "controller reset, restart transaction\n");
237 start_transaction(ec);
238 if (wait_event_timeout(ec->wait, ec_transaction_done(ec),
239 msecs_to_jiffies(ACPI_EC_DELAY)))
240 return 0;
241 }
218 /* missing GPEs, switch back to poll mode */ 242 /* missing GPEs, switch back to poll mode */
219 if (printk_ratelimit()) 243 if (printk_ratelimit())
220 pr_info(PREFIX "missing confirmations, " 244 pr_info(PREFIX "missing confirmations, "
@@ -239,10 +263,10 @@ static int ec_check_sci(struct acpi_ec *ec, u8 state)
239static int ec_poll(struct acpi_ec *ec) 263static int ec_poll(struct acpi_ec *ec)
240{ 264{
241 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY); 265 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
242 msleep(1); 266 udelay(ACPI_EC_UDELAY);
243 while (time_before(jiffies, delay)) { 267 while (time_before(jiffies, delay)) {
244 gpe_transaction(ec, acpi_ec_read_status(ec)); 268 gpe_transaction(ec, acpi_ec_read_status(ec));
245 msleep(1); 269 udelay(ACPI_EC_UDELAY);
246 if (ec_transaction_done(ec)) 270 if (ec_transaction_done(ec))
247 return 0; 271 return 0;
248 } 272 }
@@ -259,14 +283,13 @@ static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
259 /* disable GPE during transaction if storm is detected */ 283 /* disable GPE during transaction if storm is detected */
260 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) { 284 if (test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
261 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags); 285 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
262 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); 286 acpi_disable_gpe(NULL, ec->gpe);
263 } 287 }
264 /* start transaction */ 288 /* start transaction */
265 spin_lock_irqsave(&ec->curr_lock, tmp); 289 spin_lock_irqsave(&ec->curr_lock, tmp);
266 /* following two actions should be kept atomic */ 290 /* following two actions should be kept atomic */
267 t->irq_count = 0;
268 ec->curr = t; 291 ec->curr = t;
269 acpi_ec_write_cmd(ec, ec->curr->command); 292 start_transaction(ec);
270 if (ec->curr->command == ACPI_EC_COMMAND_QUERY) 293 if (ec->curr->command == ACPI_EC_COMMAND_QUERY)
271 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); 294 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
272 spin_unlock_irqrestore(&ec->curr_lock, tmp); 295 spin_unlock_irqrestore(&ec->curr_lock, tmp);
@@ -283,10 +306,11 @@ static int acpi_ec_transaction_unlocked(struct acpi_ec *ec,
283 /* check if we received SCI during transaction */ 306 /* check if we received SCI during transaction */
284 ec_check_sci(ec, acpi_ec_read_status(ec)); 307 ec_check_sci(ec, acpi_ec_read_status(ec));
285 /* it is safe to enable GPE outside of transaction */ 308 /* it is safe to enable GPE outside of transaction */
286 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); 309 acpi_enable_gpe(NULL, ec->gpe);
287 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) && 310 } else if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
288 t->irq_count > ACPI_EC_STORM_THRESHOLD) { 311 t->irq_count > ACPI_EC_STORM_THRESHOLD) {
289 pr_debug(PREFIX "GPE storm detected\n"); 312 pr_info(PREFIX "GPE storm detected, "
313 "transactions will use polling mode\n");
290 set_bit(EC_FLAGS_GPE_STORM, &ec->flags); 314 set_bit(EC_FLAGS_GPE_STORM, &ec->flags);
291 } 315 }
292 return ret; 316 return ret;
@@ -558,17 +582,26 @@ static u32 acpi_ec_gpe_handler(void *data)
558 pr_debug(PREFIX "~~~> interrupt\n"); 582 pr_debug(PREFIX "~~~> interrupt\n");
559 status = acpi_ec_read_status(ec); 583 status = acpi_ec_read_status(ec);
560 584
561 gpe_transaction(ec, status); 585 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) {
562 if (ec_transaction_done(ec) && (status & ACPI_EC_FLAG_IBF) == 0) 586 gpe_transaction(ec, status);
563 wake_up(&ec->wait); 587 if (ec_transaction_done(ec) &&
588 (status & ACPI_EC_FLAG_IBF) == 0)
589 wake_up(&ec->wait);
590 }
564 591
565 ec_check_sci(ec, status); 592 ec_check_sci(ec, status);
566 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) && 593 if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
567 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) { 594 !test_bit(EC_FLAGS_NO_GPE, &ec->flags)) {
568 /* this is non-query, must be confirmation */ 595 /* this is non-query, must be confirmation */
569 if (printk_ratelimit()) 596 if (!test_bit(EC_FLAGS_GPE_STORM, &ec->flags)) {
570 pr_info(PREFIX "non-query interrupt received," 597 if (printk_ratelimit())
598 pr_info(PREFIX "non-query interrupt received,"
599 " switching to interrupt mode\n");
600 } else {
601 /* hush, STORM switches the mode every transaction */
602 pr_debug(PREFIX "non-query interrupt received,"
571 " switching to interrupt mode\n"); 603 " switching to interrupt mode\n");
604 }
572 set_bit(EC_FLAGS_GPE_MODE, &ec->flags); 605 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
573 } 606 }
574 return ACPI_INTERRUPT_HANDLED; 607 return ACPI_INTERRUPT_HANDLED;
@@ -736,7 +769,7 @@ static acpi_status
736ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval) 769ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
737{ 770{
738 acpi_status status; 771 acpi_status status;
739 unsigned long long tmp; 772 unsigned long long tmp = 0;
740 773
741 struct acpi_ec *ec = context; 774 struct acpi_ec *ec = context;
742 status = acpi_walk_resources(handle, METHOD_NAME__CRS, 775 status = acpi_walk_resources(handle, METHOD_NAME__CRS,
@@ -751,6 +784,7 @@ ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
751 return status; 784 return status;
752 ec->gpe = tmp; 785 ec->gpe = tmp;
753 /* Use the global lock for all EC transactions? */ 786 /* Use the global lock for all EC transactions? */
787 tmp = 0;
754 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp); 788 acpi_evaluate_integer(handle, "_GLK", NULL, &tmp);
755 ec->global_lock = tmp; 789 ec->global_lock = tmp;
756 ec->handle = handle; 790 ec->handle = handle;
@@ -868,7 +902,7 @@ static int ec_install_handlers(struct acpi_ec *ec)
868 if (ACPI_FAILURE(status)) 902 if (ACPI_FAILURE(status))
869 return -ENODEV; 903 return -ENODEV;
870 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME); 904 acpi_set_gpe_type(NULL, ec->gpe, ACPI_GPE_TYPE_RUNTIME);
871 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); 905 acpi_enable_gpe(NULL, ec->gpe);
872 status = acpi_install_address_space_handler(ec->handle, 906 status = acpi_install_address_space_handler(ec->handle,
873 ACPI_ADR_SPACE_EC, 907 ACPI_ADR_SPACE_EC,
874 &acpi_ec_space_handler, 908 &acpi_ec_space_handler,
@@ -1007,7 +1041,7 @@ static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
1007 /* Stop using GPE */ 1041 /* Stop using GPE */
1008 set_bit(EC_FLAGS_NO_GPE, &ec->flags); 1042 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
1009 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags); 1043 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
1010 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); 1044 acpi_disable_gpe(NULL, ec->gpe);
1011 return 0; 1045 return 0;
1012} 1046}
1013 1047
@@ -1016,7 +1050,7 @@ static int acpi_ec_resume(struct acpi_device *device)
1016 struct acpi_ec *ec = acpi_driver_data(device); 1050 struct acpi_ec *ec = acpi_driver_data(device);
1017 /* Enable use of GPE back */ 1051 /* Enable use of GPE back */
1018 clear_bit(EC_FLAGS_NO_GPE, &ec->flags); 1052 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
1019 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR); 1053 acpi_enable_gpe(NULL, ec->gpe);
1020 return 0; 1054 return 0;
1021} 1055}
1022 1056