summaryrefslogtreecommitdiff
path: root/common/host_event_commands.c
blob: 6fa1227272b5767079c698310369c19374fcde53 (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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/* Copyright 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Host event commands for Chrome EC */

#include "common.h"
#include "console.h"
#include "hooks.h"
#include "host_command.h"
#include "lpc.h"
#include "mkbp_event.h"
#include "power.h"
#include "system.h"
#include "task.h"
#include "util.h"

/* Console output macros */
#define CPUTS(outstr) cputs(CC_EVENTS, outstr)
#define CPRINTS(format, args...) cprints(CC_EVENTS, format, ## args)

/*
 * This is used to avoid 64-bit shifts which might require a new library
 * function.
 */
#define HOST_EVENT_32BIT_MASK(x)	(1UL << ((x) - 1))
static void host_event_set_bit(host_event_t *ev, uint8_t bit)
{
	uint32_t *ptr = (uint32_t *)ev;

	*ev = 0;

	/*
	 * Host events are 1-based, so return early if event 0 is requested to
	 * be set.
	 */
	if (bit == 0)
		return;

#ifdef CONFIG_HOST_EVENT64
	if (bit > 32)
		*(ptr + 1) = HOST_EVENT_32BIT_MASK(bit - 32);
	else
#endif
		*ptr = HOST_EVENT_32BIT_MASK(bit);
}

#ifdef CONFIG_HOSTCMD_X86

#define LPC_SYSJUMP_TAG 0x4c50  /* "LP" */
#define LPC_SYSJUMP_OLD_VERSION 1
#define LPC_SYSJUMP_VERSION 2

/*
 * Always report mask includes mask of host events that need to be reported in
 * host event always irrespective of the state of SCI, SMI and wake masks.
 *
 * Events that indicate critical shutdown/reboots that have occurred:
 *   - EC_HOST_EVENT_THERMAL_SHUTDOWN
 *   - EC_HOST_EVENT_BATTERY_SHUTDOWN
 *   - EC_HOST_EVENT_HANG_REBOOT
 *   - EC_HOST_EVENT_PANIC
 *
 * Events that are consumed by BIOS:
 *   - EC_HOST_EVENT_KEYBOARD_RECOVERY
 *   - EC_HOST_EVENT_KEYBOARD_FASTBOOT
 *   - EC_HOST_EVENT_KEYBOARD_RECOVERY_HW_REINIT
 *
 * Events that are buffered and have separate data maintained of their own:
 *   - EC_HOST_EVENT_MKBP
 *
 */
#define LPC_HOST_EVENT_ALWAYS_REPORT_DEFAULT_MASK			\
	(EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY) |		\
	 EC_HOST_EVENT_MASK(EC_HOST_EVENT_THERMAL_SHUTDOWN) |		\
	 EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_SHUTDOWN) |		\
	 EC_HOST_EVENT_MASK(EC_HOST_EVENT_HANG_REBOOT) |		\
	 EC_HOST_EVENT_MASK(EC_HOST_EVENT_PANIC) |			\
	 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_FASTBOOT) |		\
	 EC_HOST_EVENT_MASK(EC_HOST_EVENT_MKBP) |			\
	 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY_HW_REINIT))

static host_event_t lpc_host_events;
static host_event_t lpc_host_event_mask[LPC_HOST_EVENT_COUNT];

/* Indicates if active wake mask set by host */
static uint8_t active_wm_set_by_host;

void lpc_set_host_event_mask(enum lpc_host_event_type type, host_event_t mask)
{
	lpc_host_event_mask[type] = mask;
	lpc_update_host_event_status();

	/* mask 0 indicates wake mask not set by host */
	if ((type == LPC_HOST_EVENT_WAKE) && (mask == 0))
		active_wm_set_by_host = 0;
}

host_event_t lpc_get_host_event_mask(enum lpc_host_event_type type)
{
	return lpc_host_event_mask[type];
}

static host_event_t lpc_get_all_host_event_masks(void)
{
	host_event_t or_mask = 0;
	int i;

	for (i = 0; i < LPC_HOST_EVENT_COUNT; i++)
		or_mask |= lpc_get_host_event_mask(i);

	return or_mask;
}

static void lpc_set_host_event_state(host_event_t events)
{
	if (events == lpc_host_events)
		return;

	lpc_host_events = events;
	lpc_update_host_event_status();
}

host_event_t lpc_get_host_events_by_type(enum lpc_host_event_type type)
{
	return lpc_host_events & lpc_get_host_event_mask(type);
}

host_event_t lpc_get_host_events(void)
{
	return lpc_host_events;
}

int lpc_get_next_host_event(void)
{
	host_event_t ev;
	int evt_idx =  __builtin_ffs(lpc_host_events);

#ifdef CONFIG_HOST_EVENT64
	if (evt_idx == 0) {
		int evt_idx_high = __builtin_ffs(lpc_host_events >> 32);

		if (evt_idx_high)
			evt_idx = 32 + evt_idx_high;
	}
#endif

	if (evt_idx) {
		host_event_set_bit(&ev, evt_idx);
		host_clear_events(ev);
	}
	return evt_idx;
}

static void lpc_sysjump_save_mask(void)
{
	system_add_jump_tag(LPC_SYSJUMP_TAG, LPC_SYSJUMP_VERSION,
			    sizeof(lpc_host_event_mask), lpc_host_event_mask);
}
DECLARE_HOOK(HOOK_SYSJUMP, lpc_sysjump_save_mask, HOOK_PRIO_DEFAULT);

/*
 * Restore various LPC masks if they were saved before the sysjump.
 *
 * Returns:
 * 1 = All masks were restored
 * 0 = No masks were stashed before sysjump or EC performing sysjump did not
 *     support always report mask.
 */
static int lpc_post_sysjump_restore_mask(void)
{
	const host_event_t *prev_mask;
	int size, version;

	prev_mask = (const host_event_t *)system_get_jump_tag(LPC_SYSJUMP_TAG,
			&version, &size);
	if (!prev_mask || size != sizeof(lpc_host_event_mask) ||
	    (version != LPC_SYSJUMP_VERSION &&
	     version != LPC_SYSJUMP_OLD_VERSION))
		return 0;

	memcpy(lpc_host_event_mask, prev_mask, sizeof(lpc_host_event_mask));

	return version == LPC_SYSJUMP_VERSION;
}

host_event_t __attribute__((weak)) lpc_override_always_report_mask(void)
{
	return LPC_HOST_EVENT_ALWAYS_REPORT_DEFAULT_MASK;
}

void lpc_init_mask(void)
{
	/*
	 * First check if masks were stashed before sysjump. If no masks were
	 * stashed or if the EC image performing sysjump does not support always
	 * report mask, then set always report mask now.
	 */
	if (!lpc_post_sysjump_restore_mask())
		lpc_host_event_mask[LPC_HOST_EVENT_ALWAYS_REPORT] =
			lpc_override_always_report_mask();
}

void lpc_s3_resume_clear_masks(void)
{
	lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, 0);
	lpc_set_host_event_mask(LPC_HOST_EVENT_SCI, 0);
	lpc_set_host_event_mask(LPC_HOST_EVENT_WAKE, 0);
}

/*
 * Clear events that are not part of SCI/SMI mask so as to prevent
 * premature wakes on next suspend(S0ix). This is not needed on
 * suspending to S3 as coreboot clears all events on path to suspend.
 *
 * We preserve events that are part of SCI/SMI mask to help kernel
 * identify the wake reason on resume. For events that are not set
 * in SCI mask but are part of S0iX WAKE masks, kernel drivers should
 * have other ways (physical/virtual interrupt) pin to identify when
 * they trigger wakes.
 */
#ifdef CONFIG_POWER_S0IX
void clear_non_sci_events(void)
{
	host_clear_events(~lpc_get_host_event_mask(LPC_HOST_EVENT_SCI) &
			  ~lpc_get_host_event_mask(LPC_HOST_EVENT_SMI));
}
DECLARE_HOOK(HOOK_CHIPSET_RESUME, clear_non_sci_events, HOOK_PRIO_DEFAULT);
#endif

#endif

/*
 * Maintain two copies of the events that are set.
 *
 * The primary copy is mirrored in mapped memory and used to trigger interrupts
 * on the host via ACPI/SCI/SMI/GPIO.
 *
 * The secondary (B) copy is used by entities other than ACPI to query the state
 * of host events on EC. Currently events_copy_b is used for
 *      1. Logging recovery mode switch in coreboot.
 *      2. Used by depthcharge on devices with no 8042 and no MKBP interrupt.
 *      3. Logging wake reason in coreboot.
 * Current query of a event from copy_b is immediately followed by clear of the
 * same event. Further uses of copy_b should make sure this semantics is
 * followed and none of the above mentioned use cases are broken.
 *
 * Setting an event sets both copies.  Copies are cleared separately.
 */
static host_event_t events;
static host_event_t events_copy_b;

/* Lazy wake masks */
#ifdef CONFIG_HOSTCMD_X86
static struct lazy_wake_masks {
	host_event_t s3_lazy_wm;
	host_event_t s5_lazy_wm;
#ifdef CONFIG_POWER_S0IX
	host_event_t s0ix_lazy_wm;
#endif
} lazy_wm;
#endif

static void host_events_atomic_or(host_event_t *e, host_event_t m)
{
	atomic_t *ptr = (atomic_t *)e;

	atomic_or(ptr, (uint32_t)m);
#ifdef CONFIG_HOST_EVENT64
	atomic_or(ptr + 1, (uint32_t)(m >> 32));
#endif
}

static void host_events_atomic_clear(host_event_t *e, host_event_t m)
{
	atomic_t *ptr = (atomic_t *)e;

	atomic_clear_bits(ptr, (uint32_t)m);
#ifdef CONFIG_HOST_EVENT64
	atomic_clear_bits(ptr + 1, (uint32_t)(m >> 32));
#endif
}

#if !defined(CONFIG_HOSTCMD_X86) && defined(CONFIG_MKBP_EVENT)
static void host_events_send_mkbp_event(host_event_t e)
{
#ifdef CONFIG_HOST_EVENT64
	/*
	 * If event bits in the upper 32-bit are set, indicate 64-bit host
	 * event.
	 */
	if (!(uint32_t)e)
		mkbp_send_event(EC_MKBP_EVENT_HOST_EVENT64);
	else
#endif
		mkbp_send_event(EC_MKBP_EVENT_HOST_EVENT);
}
#endif

host_event_t host_get_events(void)
{
	return events;
}

void host_set_events(host_event_t mask)
{
	/* ignore host events the rest of board doesn't care about */
#ifdef CONFIG_HOST_EVENT64
	mask &= CONFIG_HOST_EVENT64_REPORT_MASK;
#else
	mask &= CONFIG_HOST_EVENT_REPORT_MASK;
#endif

#ifdef CONFIG_HOSTCMD_X86
	/*
	 * Host only cares about the events for which the masks are set either
	 * in wake mask, SCI mask or SMI mask. In addition to that, there are
	 * certain events that need to be always reported (Please see
	 * LPC_HOST_EVENT_ALWAYS_REPORT_DEFAULT_MASK). Thus, when a new host
	 * event is being set, ensure that it is present in one of these
	 * masks. Else, there is no need to process that event.
	 */
	mask &= lpc_get_all_host_event_masks();
#endif

	/* exit now if nothing has changed */
	if (!((events & mask) != mask || (events_copy_b & mask) != mask))
		return;

	HOST_EVENT_CPRINTS("event set", mask);

	if (!IS_ENABLED(CONFIG_ZTEST) &&
		(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY)))
		system_enter_manual_recovery();

	host_events_atomic_or(&events, mask);
	host_events_atomic_or(&events_copy_b, mask);

#ifdef CONFIG_HOSTCMD_X86
	lpc_set_host_event_state(events);
#else
	*(host_event_t *)host_get_memmap(EC_MEMMAP_HOST_EVENTS) = events;
#ifdef CONFIG_MKBP_EVENT
#ifdef CONFIG_MKBP_USE_HOST_EVENT
#error "Config error: MKBP must not be on top of host event"
#endif
	host_events_send_mkbp_event(events);
#endif  /* CONFIG_MKBP_EVENT */
#endif  /* !CONFIG_HOSTCMD_X86 */
}

void host_set_single_event(enum host_event_code event)
{
	host_event_t ev = 0;

	host_event_set_bit(&ev, event);
	host_set_events(ev);
}

int host_is_event_set(enum host_event_code event)
{
	host_event_t ev = 0;

	host_event_set_bit(&ev, event);
	return events & ev;
}

void host_clear_events(host_event_t mask)
{
	/* ignore host events the rest of board doesn't care about */
#ifdef CONFIG_HOST_EVENT64
	mask &= CONFIG_HOST_EVENT64_REPORT_MASK;
#else
	mask &= CONFIG_HOST_EVENT_REPORT_MASK;
#endif

	/* return early if nothing changed */
	if (!(events & mask))
		return;

	HOST_EVENT_CPRINTS("event clear", mask);

	host_events_atomic_clear(&events, mask);

#ifdef CONFIG_HOSTCMD_X86
	lpc_set_host_event_state(events);
#else
	*(host_event_t *)host_get_memmap(EC_MEMMAP_HOST_EVENTS) = events;
#ifdef CONFIG_MKBP_EVENT
	host_events_send_mkbp_event(events);
#endif
#endif  /* !CONFIG_HOSTCMD_X86 */
}

#ifndef CONFIG_HOSTCMD_X86
static int host_get_next_event(uint8_t *out)
{
	uint32_t event_out = (uint32_t)events;
	memcpy(out, &event_out, sizeof(event_out));
	host_events_atomic_clear(&events, event_out);
	*(host_event_t *)host_get_memmap(EC_MEMMAP_HOST_EVENTS) = events;
	return sizeof(event_out);
}
DECLARE_EVENT_SOURCE(EC_MKBP_EVENT_HOST_EVENT, host_get_next_event);

#ifdef CONFIG_HOST_EVENT64
static int host_get_next_event64(uint8_t *out)
{
	host_event_t event_out = events;

	memcpy(out, &event_out, sizeof(event_out));
	host_events_atomic_clear(&events, event_out);
	*(host_event_t *)host_get_memmap(EC_MEMMAP_HOST_EVENTS) = events;
	return sizeof(event_out);
}
DECLARE_EVENT_SOURCE(EC_MKBP_EVENT_HOST_EVENT64, host_get_next_event64);
#endif
#endif

/**
 * Clear one or more host event bits from copy B.
 *
 * @param mask          Event bits to clear (use EC_HOST_EVENT_MASK()).
 *                      Write 1 to a bit to clear it.
 */
static void host_clear_events_b(host_event_t mask)
{
	/* Only print if something's about to change */
	if (events_copy_b & mask)
		HOST_EVENT_CPRINTS("event clear B", mask);

	host_events_atomic_clear(&events_copy_b, mask);
}

/**
 * Politely ask the CPU to enable/disable its own throttling.
 *
 * @param throttle	Enable (!=0) or disable(0) throttling
 */
test_mockable void host_throttle_cpu(int throttle)
{
	if (throttle)
		host_set_single_event(EC_HOST_EVENT_THROTTLE_START);
	else
		host_set_single_event(EC_HOST_EVENT_THROTTLE_STOP);
}

/*
 * Events copy b is used by coreboot for logging the wake reason. For this to
 * work, events_copy_b needs to be cleared on every suspend.
 */
void clear_events_copy_b(void)
{
	events_copy_b = 0;
}
DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, clear_events_copy_b, HOOK_PRIO_DEFAULT);

/*****************************************************************************/
/* Console commands */
static int command_host_event(int argc, char **argv)
{
	/* Handle sub-commands */
	if (argc == 3) {
		char *e;
		host_event_t i = strtoull(argv[2], &e, 0);
		if (*e)
			return EC_ERROR_PARAM2;

		if (!strcasecmp(argv[1], "set"))
			host_set_events(i);
		else if (!strcasecmp(argv[1], "clear"))
			host_clear_events(i);
		else if (!strcasecmp(argv[1], "clearb"))
			host_clear_events_b(i);
#ifdef CONFIG_HOSTCMD_X86
		else if (!strcasecmp(argv[1], "smi"))
			lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, i);
		else if (!strcasecmp(argv[1], "sci"))
			lpc_set_host_event_mask(LPC_HOST_EVENT_SCI, i);
		else if (!strcasecmp(argv[1], "wake"))
			lpc_set_host_event_mask(LPC_HOST_EVENT_WAKE, i);
		else if (!strcasecmp(argv[1], "always_report"))
			lpc_set_host_event_mask(LPC_HOST_EVENT_ALWAYS_REPORT,
						i);
#endif
		else
			return EC_ERROR_PARAM1;
	}

	/* Print current SMI/SCI status */
	HOST_EVENT_CCPRINTF("Events:             ", host_get_events());
	HOST_EVENT_CCPRINTF("Events-B:           ", events_copy_b);
#ifdef CONFIG_HOSTCMD_X86
	HOST_EVENT_CCPRINTF("SMI mask:           ",
		 lpc_get_host_event_mask(LPC_HOST_EVENT_SMI));
	HOST_EVENT_CCPRINTF("SCI mask:           ",
		 lpc_get_host_event_mask(LPC_HOST_EVENT_SCI));
	HOST_EVENT_CCPRINTF("Wake mask:          ",
		 lpc_get_host_event_mask(LPC_HOST_EVENT_WAKE));
	HOST_EVENT_CCPRINTF("Always report mask: ",
		 lpc_get_host_event_mask(LPC_HOST_EVENT_ALWAYS_REPORT));
#endif
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(hostevent, command_host_event,
			"[set | clear | clearb | smi | sci | wake | always_report] [mask]",
			"Print / set host event state");

/*****************************************************************************/
/* Host commands */

#ifdef CONFIG_HOSTCMD_X86

static enum ec_status
host_event_get_smi_mask(struct host_cmd_handler_args *args)
{
	struct ec_response_host_event_mask *r = args->response;

	r->mask = (uint32_t)lpc_get_host_event_mask(LPC_HOST_EVENT_SMI);
	args->response_size = sizeof(*r);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_GET_SMI_MASK,
		     host_event_get_smi_mask,
		     EC_VER_MASK(0));

static enum ec_status
host_event_get_sci_mask(struct host_cmd_handler_args *args)
{
	struct ec_response_host_event_mask *r = args->response;

	r->mask = (uint32_t)lpc_get_host_event_mask(LPC_HOST_EVENT_SCI);
	args->response_size = sizeof(*r);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_GET_SCI_MASK,
		     host_event_get_sci_mask,
		     EC_VER_MASK(0));

static enum ec_status
host_event_get_wake_mask(struct host_cmd_handler_args *args)
{
	struct ec_response_host_event_mask *r = args->response;

	r->mask = (uint32_t)lpc_get_host_event_mask(LPC_HOST_EVENT_WAKE);
	args->response_size = sizeof(*r);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_GET_WAKE_MASK,
		     host_event_get_wake_mask,
		     EC_VER_MASK(0));

static enum ec_status
host_event_set_smi_mask(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event_mask *p = args->params;

	lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, p->mask);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_SET_SMI_MASK,
		     host_event_set_smi_mask,
		     EC_VER_MASK(0));

static enum ec_status
host_event_set_sci_mask(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event_mask *p = args->params;

	lpc_set_host_event_mask(LPC_HOST_EVENT_SCI, p->mask);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_SET_SCI_MASK,
		     host_event_set_sci_mask,
		     EC_VER_MASK(0));

static enum ec_status
host_event_set_wake_mask(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event_mask *p = args->params;

	lpc_set_host_event_mask(LPC_HOST_EVENT_WAKE, p->mask);
	active_wm_set_by_host = !!p->mask;
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_SET_WAKE_MASK,
		     host_event_set_wake_mask,
		     EC_VER_MASK(0));

uint8_t lpc_is_active_wm_set_by_host(void)
{
	return active_wm_set_by_host;
}

#endif  /* CONFIG_HOSTCMD_X86 */

static enum ec_status host_event_get_b(struct host_cmd_handler_args *args)
{
	struct ec_response_host_event_mask *r = args->response;

	r->mask = events_copy_b;
	args->response_size = sizeof(*r);

	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_GET_B,
		     host_event_get_b,
		     EC_VER_MASK(0));

static enum ec_status host_event_clear(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event_mask *p = args->params;

	host_clear_events(p->mask);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_CLEAR,
		     host_event_clear,
		     EC_VER_MASK(0));

static enum ec_status host_event_clear_b(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event_mask *p = args->params;

	host_clear_events_b(p->mask);
	return EC_RES_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT_CLEAR_B,
		     host_event_clear_b,
		     EC_VER_MASK(0));

static enum ec_status host_event_action_get(struct host_cmd_handler_args *args)
{
	struct ec_response_host_event *r = args->response;
	const struct ec_params_host_event *p = args->params;
	int result = EC_RES_SUCCESS;

	args->response_size = sizeof(*r);
	memset(r, 0, sizeof(*r));

	switch (p->mask_type) {
	case EC_HOST_EVENT_MAIN:
		result = EC_RES_ACCESS_DENIED;
		break;
	case EC_HOST_EVENT_B:
		r->value = events_copy_b;
		break;
#ifdef CONFIG_HOSTCMD_X86
	case EC_HOST_EVENT_SCI_MASK:
		r->value = lpc_get_host_event_mask(LPC_HOST_EVENT_SCI);
		break;
	case EC_HOST_EVENT_SMI_MASK:
		r->value = lpc_get_host_event_mask(LPC_HOST_EVENT_SMI);
		break;
	case EC_HOST_EVENT_ALWAYS_REPORT_MASK:
		r->value = lpc_get_host_event_mask
				(LPC_HOST_EVENT_ALWAYS_REPORT);
		break;
	case EC_HOST_EVENT_ACTIVE_WAKE_MASK:
		r->value = lpc_get_host_event_mask(LPC_HOST_EVENT_WAKE);
		break;
#ifdef CONFIG_POWER_S0IX
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX:
		r->value = lazy_wm.s0ix_lazy_wm;
		break;
#endif
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S3:
		r->value = lazy_wm.s3_lazy_wm;
		break;
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S5:
		r->value = lazy_wm.s5_lazy_wm;
		break;
#endif
	default:
		result = EC_RES_INVALID_PARAM;
		break;
	}

	return result;
}

static enum ec_status host_event_action_set(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event *p = args->params;
	int result = EC_RES_SUCCESS;
	host_event_t mask_value __unused = (host_event_t)(p->value);

	switch (p->mask_type) {
	case EC_HOST_EVENT_MAIN:
	case EC_HOST_EVENT_B:
		result = EC_RES_ACCESS_DENIED;
		break;
#ifdef CONFIG_HOSTCMD_X86
	case EC_HOST_EVENT_SCI_MASK:
		lpc_set_host_event_mask(LPC_HOST_EVENT_SCI, mask_value);
		break;
	case EC_HOST_EVENT_SMI_MASK:
		lpc_set_host_event_mask(LPC_HOST_EVENT_SMI, mask_value);
		break;
	case EC_HOST_EVENT_ALWAYS_REPORT_MASK:
		lpc_set_host_event_mask(LPC_HOST_EVENT_ALWAYS_REPORT,
						mask_value);
		break;
	case EC_HOST_EVENT_ACTIVE_WAKE_MASK:
		active_wm_set_by_host = !!mask_value;
		lpc_set_host_event_mask(LPC_HOST_EVENT_WAKE, mask_value);
		break;
#ifdef CONFIG_POWER_S0IX
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX:
		lazy_wm.s0ix_lazy_wm = mask_value;
		break;
#endif
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S3:
		lazy_wm.s3_lazy_wm = mask_value;
		break;
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S5:
		lazy_wm.s5_lazy_wm = mask_value;
		break;
#endif
	default:
		result = EC_RES_INVALID_PARAM;
		break;
	}

	return result;
}

static enum ec_status
host_event_action_clear(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event *p = args->params;
	int result = EC_RES_SUCCESS;
	host_event_t mask_value = (host_event_t)(p->value);

	switch (p->mask_type) {
	case EC_HOST_EVENT_MAIN:
		host_clear_events(mask_value);
		break;
	case EC_HOST_EVENT_B:
		host_clear_events_b(mask_value);
		break;
#ifdef CONFIG_HOSTCMD_X86
	case EC_HOST_EVENT_SCI_MASK:
	case EC_HOST_EVENT_SMI_MASK:
	case EC_HOST_EVENT_ALWAYS_REPORT_MASK:
	case EC_HOST_EVENT_ACTIVE_WAKE_MASK:
#ifdef CONFIG_POWER_S0IX
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX:
#endif
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S3:
	case EC_HOST_EVENT_LAZY_WAKE_MASK_S5:
		result = EC_RES_ACCESS_DENIED;
		break;
#endif
	default:
		result = EC_RES_INVALID_PARAM;
	}

	return result;
}

static enum ec_status
host_command_host_event(struct host_cmd_handler_args *args)
{
	const struct ec_params_host_event *p = args->params;

	args->response_size = 0;

	switch (p->action) {
	case EC_HOST_EVENT_GET:
		return host_event_action_get(args);
	case EC_HOST_EVENT_SET:
		return host_event_action_set(args);
	case EC_HOST_EVENT_CLEAR:
		return host_event_action_clear(args);
	default:
		return EC_RES_INVALID_PARAM;
	}
}

DECLARE_HOST_COMMAND(EC_CMD_HOST_EVENT,
		     host_command_host_event,
		     EC_VER_MASK(0));

#define LAZY_WAKE_MASK_SYSJUMP_TAG		0x4C4D /* LM - Lazy Mask*/
#define LAZY_WAKE_MASK_HOOK_VERSION		1

#ifdef CONFIG_HOSTCMD_X86
int get_lazy_wake_mask(enum power_state state, host_event_t *mask)
{
	int ret = EC_SUCCESS;

	switch (state) {
	case POWER_S5:
		*mask = lazy_wm.s5_lazy_wm;
		break;
	case POWER_S3:
		*mask = lazy_wm.s3_lazy_wm;
		break;
#ifdef CONFIG_POWER_S0IX
	case POWER_S0ix:
		*mask = lazy_wm.s0ix_lazy_wm;
		break;
#endif
	default:
		*mask = 0;
		ret = EC_ERROR_INVAL;
	}

	return ret;
}

static void preserve_lazy_wm(void)
{
	system_add_jump_tag(LAZY_WAKE_MASK_SYSJUMP_TAG,
			    LAZY_WAKE_MASK_HOOK_VERSION,
			    sizeof(lazy_wm),
			    &lazy_wm);
}
DECLARE_HOOK(HOOK_SYSJUMP, preserve_lazy_wm, HOOK_PRIO_DEFAULT);

static void restore_lazy_wm(void)
{
	const struct lazy_wake_masks *wm_state;
	int version, size;

	wm_state = (const struct lazy_wake_masks *)
			system_get_jump_tag(LAZY_WAKE_MASK_SYSJUMP_TAG,
				 &version, &size);

	if (wm_state && (version == LAZY_WAKE_MASK_HOOK_VERSION) &&
	    (size == sizeof(lazy_wm))) {
		lazy_wm = *wm_state;
	}
}
DECLARE_HOOK(HOOK_INIT, restore_lazy_wm, HOOK_PRIO_POST_CHIPSET);
#endif