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.c240
1 files changed, 124 insertions, 116 deletions
diff --git a/drivers/acpi/ec.c b/drivers/acpi/ec.c
index 7222a18a0319..3d9362140831 100644
--- a/drivers/acpi/ec.c
+++ b/drivers/acpi/ec.c
@@ -73,38 +73,14 @@ enum ec_event {
73 73
74#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */ 74#define ACPI_EC_DELAY 500 /* Wait 500ms max. during EC ops */
75#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */ 75#define ACPI_EC_UDELAY_GLK 1000 /* Wait 1ms max. to get global lock */
76#define ACPI_EC_UDELAY 100 /* Wait 100us before polling EC again */
76 77
77enum { 78enum {
78 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */ 79 EC_FLAGS_WAIT_GPE = 0, /* Don't check status until GPE arrives */
79 EC_FLAGS_QUERY_PENDING, /* Query is pending */ 80 EC_FLAGS_QUERY_PENDING, /* Query is pending */
80 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */ 81 EC_FLAGS_GPE_MODE, /* Expect GPE to be sent for status change */
81 EC_FLAGS_NO_ADDRESS_GPE, /* Expect GPE only for non-address event */ 82 EC_FLAGS_NO_GPE, /* Don't use GPE mode */
82 EC_FLAGS_ADDRESS, /* Address is being written */ 83 EC_FLAGS_RESCHEDULE_POLL /* Re-schedule poll */
83 EC_FLAGS_NO_WDATA_GPE, /* Don't expect WDATA GPE event */
84 EC_FLAGS_WDATA, /* Data is being written */
85 EC_FLAGS_NO_OBF1_GPE, /* Don't expect GPE before read */
86};
87
88static int acpi_ec_remove(struct acpi_device *device, int type);
89static int acpi_ec_start(struct acpi_device *device);
90static int acpi_ec_stop(struct acpi_device *device, int type);
91static int acpi_ec_add(struct acpi_device *device);
92
93static const struct acpi_device_id ec_device_ids[] = {
94 {"PNP0C09", 0},
95 {"", 0},
96};
97
98static struct acpi_driver acpi_ec_driver = {
99 .name = "ec",
100 .class = ACPI_EC_CLASS,
101 .ids = ec_device_ids,
102 .ops = {
103 .add = acpi_ec_add,
104 .remove = acpi_ec_remove,
105 .start = acpi_ec_start,
106 .stop = acpi_ec_stop,
107 },
108}; 84};
109 85
110/* If we find an EC via the ECDT, we need to keep a ptr to its context */ 86/* If we find an EC via the ECDT, we need to keep a ptr to its context */
@@ -129,6 +105,8 @@ static struct acpi_ec {
129 struct mutex lock; 105 struct mutex lock;
130 wait_queue_head_t wait; 106 wait_queue_head_t wait;
131 struct list_head list; 107 struct list_head list;
108 struct delayed_work work;
109 atomic_t irq_count;
132 u8 handlers_installed; 110 u8 handlers_installed;
133} *boot_ec, *first_ec; 111} *boot_ec, *first_ec;
134 112
@@ -177,65 +155,52 @@ static inline int acpi_ec_check_status(struct acpi_ec *ec, enum ec_event event)
177 return 0; 155 return 0;
178} 156}
179 157
180static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll) 158static void ec_schedule_ec_poll(struct acpi_ec *ec)
181{ 159{
182 int ret = 0; 160 if (test_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags))
161 schedule_delayed_work(&ec->work,
162 msecs_to_jiffies(ACPI_EC_DELAY));
163}
183 164
184 if (unlikely(event == ACPI_EC_EVENT_OBF_1 && 165static void ec_switch_to_poll_mode(struct acpi_ec *ec)
185 test_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags))) 166{
186 force_poll = 1; 167 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
187 if (unlikely(test_bit(EC_FLAGS_ADDRESS, &ec->flags) && 168 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
188 test_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags))) 169 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
189 force_poll = 1; 170 set_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
190 if (unlikely(test_bit(EC_FLAGS_WDATA, &ec->flags) && 171}
191 test_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags))) 172
192 force_poll = 1; 173static int acpi_ec_wait(struct acpi_ec *ec, enum ec_event event, int force_poll)
174{
175 atomic_set(&ec->irq_count, 0);
193 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) && 176 if (likely(test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) &&
194 likely(!force_poll)) { 177 likely(!force_poll)) {
195 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event), 178 if (wait_event_timeout(ec->wait, acpi_ec_check_status(ec, event),
196 msecs_to_jiffies(ACPI_EC_DELAY))) 179 msecs_to_jiffies(ACPI_EC_DELAY)))
197 goto end; 180 return 0;
198 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); 181 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
199 if (acpi_ec_check_status(ec, event)) { 182 if (acpi_ec_check_status(ec, event)) {
200 if (event == ACPI_EC_EVENT_OBF_1) { 183 /* missing GPEs, switch back to poll mode */
201 /* miss OBF_1 GPE, don't expect it */ 184 if (printk_ratelimit())
202 pr_info(PREFIX "missing OBF confirmation, " 185 pr_info(PREFIX "missing confirmations, "
203 "don't expect it any longer.\n");
204 set_bit(EC_FLAGS_NO_OBF1_GPE, &ec->flags);
205 } else if (test_bit(EC_FLAGS_ADDRESS, &ec->flags)) {
206 /* miss address GPE, don't expect it anymore */
207 pr_info(PREFIX "missing address confirmation, "
208 "don't expect it any longer.\n");
209 set_bit(EC_FLAGS_NO_ADDRESS_GPE, &ec->flags);
210 } else if (test_bit(EC_FLAGS_WDATA, &ec->flags)) {
211 /* miss write data GPE, don't expect it */
212 pr_info(PREFIX "missing write data confirmation, "
213 "don't expect it any longer.\n");
214 set_bit(EC_FLAGS_NO_WDATA_GPE, &ec->flags);
215 } else {
216 /* missing GPEs, switch back to poll mode */
217 if (printk_ratelimit())
218 pr_info(PREFIX "missing confirmations, "
219 "switch off interrupt mode.\n"); 186 "switch off interrupt mode.\n");
220 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags); 187 ec_switch_to_poll_mode(ec);
221 } 188 ec_schedule_ec_poll(ec);
222 goto end; 189 return 0;
223 } 190 }
224 } else { 191 } else {
225 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY); 192 unsigned long delay = jiffies + msecs_to_jiffies(ACPI_EC_DELAY);
226 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); 193 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
227 while (time_before(jiffies, delay)) { 194 while (time_before(jiffies, delay)) {
228 if (acpi_ec_check_status(ec, event)) 195 if (acpi_ec_check_status(ec, event))
229 goto end; 196 return 0;
197 udelay(ACPI_EC_UDELAY);
230 } 198 }
231 } 199 }
232 pr_err(PREFIX "acpi_ec_wait timeout," 200 pr_err(PREFIX "acpi_ec_wait timeout, status = 0x%2.2x, event = %s\n",
233 " status = %d, expect_event = %d\n", 201 acpi_ec_read_status(ec),
234 acpi_ec_read_status(ec), event); 202 (event == ACPI_EC_EVENT_OBF_1) ? "\"b0=1\"" : "\"b1=0\"");
235 ret = -ETIME; 203 return -ETIME;
236 end:
237 clear_bit(EC_FLAGS_ADDRESS, &ec->flags);
238 return ret;
239} 204}
240 205
241static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command, 206static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
@@ -245,8 +210,8 @@ static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
245{ 210{
246 int result = 0; 211 int result = 0;
247 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); 212 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
248 acpi_ec_write_cmd(ec, command);
249 pr_debug(PREFIX "transaction start\n"); 213 pr_debug(PREFIX "transaction start\n");
214 acpi_ec_write_cmd(ec, command);
250 for (; wdata_len > 0; --wdata_len) { 215 for (; wdata_len > 0; --wdata_len) {
251 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); 216 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
252 if (result) { 217 if (result) {
@@ -254,15 +219,11 @@ static int acpi_ec_transaction_unlocked(struct acpi_ec *ec, u8 command,
254 "write_cmd timeout, command = %d\n", command); 219 "write_cmd timeout, command = %d\n", command);
255 goto end; 220 goto end;
256 } 221 }
257 /* mark the address byte written to EC */
258 if (rdata_len + wdata_len > 1)
259 set_bit(EC_FLAGS_ADDRESS, &ec->flags);
260 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags); 222 set_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
261 acpi_ec_write_data(ec, *(wdata++)); 223 acpi_ec_write_data(ec, *(wdata++));
262 } 224 }
263 225
264 if (!rdata_len) { 226 if (!rdata_len) {
265 set_bit(EC_FLAGS_WDATA, &ec->flags);
266 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll); 227 result = acpi_ec_wait(ec, ACPI_EC_EVENT_IBF_0, force_poll);
267 if (result) { 228 if (result) {
268 pr_err(PREFIX 229 pr_err(PREFIX
@@ -527,47 +488,51 @@ static u32 acpi_ec_gpe_handler(void *data)
527{ 488{
528 acpi_status status = AE_OK; 489 acpi_status status = AE_OK;
529 struct acpi_ec *ec = data; 490 struct acpi_ec *ec = data;
491 u8 state = acpi_ec_read_status(ec);
530 492
531 pr_debug(PREFIX "~~~> interrupt\n"); 493 pr_debug(PREFIX "~~~> interrupt\n");
494 atomic_inc(&ec->irq_count);
495 if (atomic_read(&ec->irq_count) > 5) {
496 pr_err(PREFIX "GPE storm detected, disabling EC GPE\n");
497 ec_switch_to_poll_mode(ec);
498 goto end;
499 }
532 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags); 500 clear_bit(EC_FLAGS_WAIT_GPE, &ec->flags);
533 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags)) 501 if (test_bit(EC_FLAGS_GPE_MODE, &ec->flags))
534 wake_up(&ec->wait); 502 wake_up(&ec->wait);
535 503
536 if (acpi_ec_read_status(ec) & ACPI_EC_FLAG_SCI) { 504 if (state & ACPI_EC_FLAG_SCI) {
537 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags)) 505 if (!test_and_set_bit(EC_FLAGS_QUERY_PENDING, &ec->flags))
538 status = acpi_os_execute(OSL_EC_BURST_HANDLER, 506 status = acpi_os_execute(OSL_EC_BURST_HANDLER,
539 acpi_ec_gpe_query, ec); 507 acpi_ec_gpe_query, ec);
540 } else if (unlikely(!test_bit(EC_FLAGS_GPE_MODE, &ec->flags))) { 508 } else if (!test_bit(EC_FLAGS_GPE_MODE, &ec->flags) &&
509 !test_bit(EC_FLAGS_NO_GPE, &ec->flags) &&
510 in_interrupt()) {
541 /* this is non-query, must be confirmation */ 511 /* this is non-query, must be confirmation */
542 if (printk_ratelimit()) 512 if (printk_ratelimit())
543 pr_info(PREFIX "non-query interrupt received," 513 pr_info(PREFIX "non-query interrupt received,"
544 " switching to interrupt mode\n"); 514 " switching to interrupt mode\n");
545 set_bit(EC_FLAGS_GPE_MODE, &ec->flags); 515 set_bit(EC_FLAGS_GPE_MODE, &ec->flags);
516 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
546 } 517 }
547 518end:
519 ec_schedule_ec_poll(ec);
548 return ACPI_SUCCESS(status) ? 520 return ACPI_SUCCESS(status) ?
549 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED; 521 ACPI_INTERRUPT_HANDLED : ACPI_INTERRUPT_NOT_HANDLED;
550} 522}
551 523
524static void do_ec_poll(struct work_struct *work)
525{
526 struct acpi_ec *ec = container_of(work, struct acpi_ec, work.work);
527 atomic_set(&ec->irq_count, 0);
528 (void)acpi_ec_gpe_handler(ec);
529}
530
552/* -------------------------------------------------------------------------- 531/* --------------------------------------------------------------------------
553 Address Space Management 532 Address Space Management
554 -------------------------------------------------------------------------- */ 533 -------------------------------------------------------------------------- */
555 534
556static acpi_status 535static acpi_status
557acpi_ec_space_setup(acpi_handle region_handle,
558 u32 function, void *handler_context, void **return_context)
559{
560 /*
561 * The EC object is in the handler context and is needed
562 * when calling the acpi_ec_space_handler.
563 */
564 *return_context = (function != ACPI_REGION_DEACTIVATE) ?
565 handler_context : NULL;
566
567 return AE_OK;
568}
569
570static acpi_status
571acpi_ec_space_handler(u32 function, acpi_physical_address address, 536acpi_ec_space_handler(u32 function, acpi_physical_address address,
572 u32 bits, acpi_integer *value, 537 u32 bits, acpi_integer *value,
573 void *handler_context, void *region_context) 538 void *handler_context, void *region_context)
@@ -709,6 +674,8 @@ static struct acpi_ec *make_acpi_ec(void)
709 mutex_init(&ec->lock); 674 mutex_init(&ec->lock);
710 init_waitqueue_head(&ec->wait); 675 init_waitqueue_head(&ec->wait);
711 INIT_LIST_HEAD(&ec->list); 676 INIT_LIST_HEAD(&ec->list);
677 INIT_DELAYED_WORK_DEFERRABLE(&ec->work, do_ec_poll);
678 atomic_set(&ec->irq_count, 0);
712 return ec; 679 return ec;
713} 680}
714 681
@@ -741,17 +708,21 @@ ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
741 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe); 708 status = acpi_evaluate_integer(handle, "_GPE", NULL, &ec->gpe);
742 if (ACPI_FAILURE(status)) 709 if (ACPI_FAILURE(status))
743 return status; 710 return status;
744 /* Find and register all query methods */
745 acpi_walk_namespace(ACPI_TYPE_METHOD, handle, 1,
746 acpi_ec_register_query_methods, ec, NULL);
747 /* Use the global lock for all EC transactions? */ 711 /* Use the global lock for all EC transactions? */
748 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock); 712 acpi_evaluate_integer(handle, "_GLK", NULL, &ec->global_lock);
749 ec->handle = handle; 713 ec->handle = handle;
750 return AE_CTRL_TERMINATE; 714 return AE_CTRL_TERMINATE;
751} 715}
752 716
717static void ec_poll_stop(struct acpi_ec *ec)
718{
719 clear_bit(EC_FLAGS_RESCHEDULE_POLL, &ec->flags);
720 cancel_delayed_work(&ec->work);
721}
722
753static void ec_remove_handlers(struct acpi_ec *ec) 723static void ec_remove_handlers(struct acpi_ec *ec)
754{ 724{
725 ec_poll_stop(ec);
755 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle, 726 if (ACPI_FAILURE(acpi_remove_address_space_handler(ec->handle,
756 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler))) 727 ACPI_ADR_SPACE_EC, &acpi_ec_space_handler)))
757 pr_err(PREFIX "failed to remove space handler\n"); 728 pr_err(PREFIX "failed to remove space handler\n");
@@ -771,31 +742,28 @@ static int acpi_ec_add(struct acpi_device *device)
771 strcpy(acpi_device_class(device), ACPI_EC_CLASS); 742 strcpy(acpi_device_class(device), ACPI_EC_CLASS);
772 743
773 /* Check for boot EC */ 744 /* Check for boot EC */
774 if (boot_ec) { 745 if (boot_ec &&
775 if (boot_ec->handle == device->handle) { 746 (boot_ec->handle == device->handle ||
776 /* Pre-loaded EC from DSDT, just move pointer */ 747 boot_ec->handle == ACPI_ROOT_OBJECT)) {
777 ec = boot_ec; 748 ec = boot_ec;
778 boot_ec = NULL; 749 boot_ec = NULL;
779 goto end; 750 } else {
780 } else if (boot_ec->handle == ACPI_ROOT_OBJECT) { 751 ec = make_acpi_ec();
781 /* ECDT-based EC, time to shut it down */ 752 if (!ec)
782 ec_remove_handlers(boot_ec); 753 return -ENOMEM;
783 kfree(boot_ec); 754 if (ec_parse_device(device->handle, 0, ec, NULL) !=
784 first_ec = boot_ec = NULL; 755 AE_CTRL_TERMINATE) {
756 kfree(ec);
757 return -EINVAL;
785 } 758 }
786 } 759 }
787 760
788 ec = make_acpi_ec();
789 if (!ec)
790 return -ENOMEM;
791
792 if (ec_parse_device(device->handle, 0, ec, NULL) !=
793 AE_CTRL_TERMINATE) {
794 kfree(ec);
795 return -EINVAL;
796 }
797 ec->handle = device->handle; 761 ec->handle = device->handle;
798 end: 762
763 /* Find and register all query methods */
764 acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
765 acpi_ec_register_query_methods, ec, NULL);
766
799 if (!first_ec) 767 if (!first_ec)
800 first_ec = ec; 768 first_ec = ec;
801 acpi_driver_data(device) = ec; 769 acpi_driver_data(device) = ec;
@@ -870,7 +838,7 @@ static int ec_install_handlers(struct acpi_ec *ec)
870 status = acpi_install_address_space_handler(ec->handle, 838 status = acpi_install_address_space_handler(ec->handle,
871 ACPI_ADR_SPACE_EC, 839 ACPI_ADR_SPACE_EC,
872 &acpi_ec_space_handler, 840 &acpi_ec_space_handler,
873 &acpi_ec_space_setup, ec); 841 NULL, ec);
874 if (ACPI_FAILURE(status)) { 842 if (ACPI_FAILURE(status)) {
875 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler); 843 acpi_remove_gpe_handler(NULL, ec->gpe, &acpi_ec_gpe_handler);
876 return -ENODEV; 844 return -ENODEV;
@@ -897,6 +865,7 @@ static int acpi_ec_start(struct acpi_device *device)
897 865
898 /* EC is fully operational, allow queries */ 866 /* EC is fully operational, allow queries */
899 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags); 867 clear_bit(EC_FLAGS_QUERY_PENDING, &ec->flags);
868 ec_schedule_ec_poll(ec);
900 return ret; 869 return ret;
901} 870}
902 871
@@ -924,6 +893,11 @@ int __init acpi_boot_ec_enable(void)
924 return -EFAULT; 893 return -EFAULT;
925} 894}
926 895
896static const struct acpi_device_id ec_device_ids[] = {
897 {"PNP0C09", 0},
898 {"", 0},
899};
900
927int __init acpi_ec_ecdt_probe(void) 901int __init acpi_ec_ecdt_probe(void)
928{ 902{
929 int ret; 903 int ret;
@@ -944,6 +918,7 @@ int __init acpi_ec_ecdt_probe(void)
944 boot_ec->data_addr = ecdt_ptr->data.address; 918 boot_ec->data_addr = ecdt_ptr->data.address;
945 boot_ec->gpe = ecdt_ptr->gpe; 919 boot_ec->gpe = ecdt_ptr->gpe;
946 boot_ec->handle = ACPI_ROOT_OBJECT; 920 boot_ec->handle = ACPI_ROOT_OBJECT;
921 acpi_get_handle(ACPI_ROOT_OBJECT, ecdt_ptr->id, &boot_ec->handle);
947 } else { 922 } else {
948 /* This workaround is needed only on some broken machines, 923 /* This workaround is needed only on some broken machines,
949 * which require early EC, but fail to provide ECDT */ 924 * which require early EC, but fail to provide ECDT */
@@ -973,6 +948,39 @@ int __init acpi_ec_ecdt_probe(void)
973 return -ENODEV; 948 return -ENODEV;
974} 949}
975 950
951static int acpi_ec_suspend(struct acpi_device *device, pm_message_t state)
952{
953 struct acpi_ec *ec = acpi_driver_data(device);
954 /* Stop using GPE */
955 set_bit(EC_FLAGS_NO_GPE, &ec->flags);
956 clear_bit(EC_FLAGS_GPE_MODE, &ec->flags);
957 acpi_disable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
958 return 0;
959}
960
961static int acpi_ec_resume(struct acpi_device *device)
962{
963 struct acpi_ec *ec = acpi_driver_data(device);
964 /* Enable use of GPE back */
965 clear_bit(EC_FLAGS_NO_GPE, &ec->flags);
966 acpi_enable_gpe(NULL, ec->gpe, ACPI_NOT_ISR);
967 return 0;
968}
969
970static struct acpi_driver acpi_ec_driver = {
971 .name = "ec",
972 .class = ACPI_EC_CLASS,
973 .ids = ec_device_ids,
974 .ops = {
975 .add = acpi_ec_add,
976 .remove = acpi_ec_remove,
977 .start = acpi_ec_start,
978 .stop = acpi_ec_stop,
979 .suspend = acpi_ec_suspend,
980 .resume = acpi_ec_resume,
981 },
982};
983
976static int __init acpi_ec_init(void) 984static int __init acpi_ec_init(void)
977{ 985{
978 int result = 0; 986 int result = 0;