Skip to content

Commit da35182

Browse files
James Morsectmarinas
authored andcommitted
firmware: arm_sdei: Add support for CPU and system power states
When a CPU enters an idle lower-power state or is powering off, we need to mask SDE events so that no events can be delivered while we are messing with the MMU as the registered entry points won't be valid. If the system reboots, we want to unregister all events and mask the CPUs. For kexec this allows us to hand a clean slate to the next kernel instead of relying on it to call sdei_{private,system}_data_reset(). For hibernate we unregister all events and re-register them on restore, in case we restored with the SDE code loaded at a different address. (e.g. KASLR). Add all the notifiers necessary to do this. We only support shared events so all events are left registered and enabled over CPU hotplug. Reviewed-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> Signed-off-by: James Morse <james.morse@arm.com> [catalin.marinas@arm.com: added CPU_PM_ENTER_FAILED case] Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent f5df269 commit da35182

2 files changed

Lines changed: 256 additions & 1 deletion

File tree

‎drivers/firmware/arm_sdei.c‎

Lines changed: 255 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
#include <linux/arm-smccc.h>
88
#include <linux/bitops.h>
99
#include <linux/compiler.h>
10+
#include <linux/cpuhotplug.h>
11+
#include <linux/cpu_pm.h>
1012
#include <linux/errno.h>
1113
#include <linux/hardirq.h>
1214
#include <linux/kernel.h>
1315
#include <linux/kprobes.h>
1416
#include <linux/kvm_host.h>
1517
#include <linux/list.h>
1618
#include <linux/mutex.h>
19+
#include <linux/notifier.h>
1720
#include <linux/of.h>
1821
#include <linux/of_platform.h>
1922
#include <linux/percpu.h>
2023
#include <linux/platform_device.h>
24+
#include <linux/pm.h>
2125
#include <linux/ptrace.h>
2226
#include <linux/preempt.h>
27+
#include <linux/reboot.h>
2328
#include <linux/slab.h>
2429
#include <linux/smp.h>
2530
#include <linux/spinlock.h>
@@ -37,7 +42,11 @@ static asmlinkage void (*sdei_firmware_call)(unsigned long function_id,
3742
static unsigned long sdei_entry_point;
3843

3944
struct sdei_event {
45+
/* These three are protected by the sdei_list_lock */
4046
struct list_head list;
47+
bool reregister;
48+
bool reenable;
49+
4150
u32 event_num;
4251
u8 type;
4352
u8 priority;
@@ -253,6 +262,11 @@ static void _ipi_mask_cpu(void *ignored)
253262
sdei_mask_local_cpu();
254263
}
255264

265+
static int sdei_cpuhp_down(unsigned int ignored)
266+
{
267+
return sdei_mask_local_cpu();
268+
}
269+
256270
int sdei_unmask_local_cpu(void)
257271
{
258272
int err;
@@ -274,6 +288,11 @@ static void _ipi_unmask_cpu(void *ignored)
274288
sdei_unmask_local_cpu();
275289
}
276290

291+
static int sdei_cpuhp_up(unsigned int ignored)
292+
{
293+
return sdei_unmask_local_cpu();
294+
}
295+
277296
static void _ipi_private_reset(void *ignored)
278297
{
279298
int err;
@@ -330,6 +349,10 @@ int sdei_event_enable(u32 event_num)
330349
return -ENOENT;
331350
}
332351

352+
spin_lock(&sdei_list_lock);
353+
event->reenable = true;
354+
spin_unlock(&sdei_list_lock);
355+
333356
if (event->type == SDEI_EVENT_TYPE_SHARED)
334357
err = sdei_api_event_enable(event->event_num);
335358
mutex_unlock(&sdei_events_lock);
@@ -356,6 +379,10 @@ int sdei_event_disable(u32 event_num)
356379
return -ENOENT;
357380
}
358381

382+
spin_lock(&sdei_list_lock);
383+
event->reenable = false;
384+
spin_unlock(&sdei_list_lock);
385+
359386
if (event->type == SDEI_EVENT_TYPE_SHARED)
360387
err = sdei_api_event_disable(event->event_num);
361388
mutex_unlock(&sdei_events_lock);
@@ -374,6 +401,11 @@ static int _sdei_event_unregister(struct sdei_event *event)
374401
{
375402
lockdep_assert_held(&sdei_events_lock);
376403

404+
spin_lock(&sdei_list_lock);
405+
event->reregister = false;
406+
event->reenable = false;
407+
spin_unlock(&sdei_list_lock);
408+
377409
if (event->type == SDEI_EVENT_TYPE_SHARED)
378410
return sdei_api_event_unregister(event->event_num);
379411

@@ -408,6 +440,31 @@ int sdei_event_unregister(u32 event_num)
408440
}
409441
EXPORT_SYMBOL(sdei_event_unregister);
410442

443+
/*
444+
* unregister events, but don't destroy them as they are re-registered by
445+
* sdei_reregister_shared().
446+
*/
447+
static int sdei_unregister_shared(void)
448+
{
449+
int err = 0;
450+
struct sdei_event *event;
451+
452+
mutex_lock(&sdei_events_lock);
453+
spin_lock(&sdei_list_lock);
454+
list_for_each_entry(event, &sdei_list, list) {
455+
if (event->type != SDEI_EVENT_TYPE_SHARED)
456+
continue;
457+
458+
err = _sdei_event_unregister(event);
459+
if (err)
460+
break;
461+
}
462+
spin_unlock(&sdei_list_lock);
463+
mutex_unlock(&sdei_events_lock);
464+
465+
return err;
466+
}
467+
411468
static int sdei_api_event_register(u32 event_num, unsigned long entry_point,
412469
void *arg, u64 flags, u64 affinity)
413470
{
@@ -465,6 +522,175 @@ int sdei_event_register(u32 event_num, sdei_event_callback *cb, void *arg)
465522
}
466523
EXPORT_SYMBOL(sdei_event_register);
467524

525+
static int sdei_reregister_event(struct sdei_event *event)
526+
{
527+
int err;
528+
529+
lockdep_assert_held(&sdei_events_lock);
530+
531+
err = _sdei_event_register(event);
532+
if (err) {
533+
pr_err("Failed to re-register event %u\n", event->event_num);
534+
sdei_event_destroy(event);
535+
return err;
536+
}
537+
538+
if (event->reenable) {
539+
if (event->type == SDEI_EVENT_TYPE_SHARED)
540+
err = sdei_api_event_enable(event->event_num);
541+
}
542+
543+
if (err)
544+
pr_err("Failed to re-enable event %u\n", event->event_num);
545+
546+
return err;
547+
}
548+
549+
static int sdei_reregister_shared(void)
550+
{
551+
int err = 0;
552+
struct sdei_event *event;
553+
554+
mutex_lock(&sdei_events_lock);
555+
spin_lock(&sdei_list_lock);
556+
list_for_each_entry(event, &sdei_list, list) {
557+
if (event->type != SDEI_EVENT_TYPE_SHARED)
558+
continue;
559+
560+
if (event->reregister) {
561+
err = sdei_reregister_event(event);
562+
if (err)
563+
break;
564+
}
565+
}
566+
spin_unlock(&sdei_list_lock);
567+
mutex_unlock(&sdei_events_lock);
568+
569+
return err;
570+
}
571+
572+
/* When entering idle, mask/unmask events for this cpu */
573+
static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action,
574+
void *data)
575+
{
576+
int rv;
577+
578+
switch (action) {
579+
case CPU_PM_ENTER:
580+
rv = sdei_mask_local_cpu();
581+
break;
582+
case CPU_PM_EXIT:
583+
case CPU_PM_ENTER_FAILED:
584+
rv = sdei_unmask_local_cpu();
585+
break;
586+
default:
587+
return NOTIFY_DONE;
588+
}
589+
590+
if (rv)
591+
return notifier_from_errno(rv);
592+
593+
return NOTIFY_OK;
594+
}
595+
596+
static struct notifier_block sdei_pm_nb = {
597+
.notifier_call = sdei_pm_notifier,
598+
};
599+
600+
static int sdei_device_suspend(struct device *dev)
601+
{
602+
on_each_cpu(_ipi_mask_cpu, NULL, true);
603+
604+
return 0;
605+
}
606+
607+
static int sdei_device_resume(struct device *dev)
608+
{
609+
on_each_cpu(_ipi_unmask_cpu, NULL, true);
610+
611+
return 0;
612+
}
613+
614+
/*
615+
* We need all events to be reregistered when we resume from hibernate.
616+
*
617+
* The sequence is freeze->thaw. Reboot. freeze->restore. We unregister
618+
* events during freeze, then re-register and re-enable them during thaw
619+
* and restore.
620+
*/
621+
static int sdei_device_freeze(struct device *dev)
622+
{
623+
int err;
624+
625+
cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING);
626+
627+
err = sdei_unregister_shared();
628+
if (err)
629+
return err;
630+
631+
return 0;
632+
}
633+
634+
static int sdei_device_thaw(struct device *dev)
635+
{
636+
int err;
637+
638+
/* re-register shared events */
639+
err = sdei_reregister_shared();
640+
if (err) {
641+
pr_warn("Failed to re-register shared events...\n");
642+
sdei_mark_interface_broken();
643+
return err;
644+
}
645+
646+
err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI",
647+
&sdei_cpuhp_up, &sdei_cpuhp_down);
648+
if (err)
649+
pr_warn("Failed to re-register CPU hotplug notifier...\n");
650+
651+
return err;
652+
}
653+
654+
static int sdei_device_restore(struct device *dev)
655+
{
656+
int err;
657+
658+
err = sdei_platform_reset();
659+
if (err)
660+
return err;
661+
662+
return sdei_device_thaw(dev);
663+
}
664+
665+
static const struct dev_pm_ops sdei_pm_ops = {
666+
.suspend = sdei_device_suspend,
667+
.resume = sdei_device_resume,
668+
.freeze = sdei_device_freeze,
669+
.thaw = sdei_device_thaw,
670+
.restore = sdei_device_restore,
671+
};
672+
673+
/*
674+
* Mask all CPUs and unregister all events on panic, reboot or kexec.
675+
*/
676+
static int sdei_reboot_notifier(struct notifier_block *nb, unsigned long action,
677+
void *data)
678+
{
679+
/*
680+
* We are going to reset the interface, after this there is no point
681+
* doing work when we take CPUs offline.
682+
*/
683+
cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING);
684+
685+
sdei_platform_reset();
686+
687+
return NOTIFY_OK;
688+
}
689+
690+
static struct notifier_block sdei_reboot_nb = {
691+
.notifier_call = sdei_reboot_notifier,
692+
};
693+
468694
static void sdei_smccc_smc(unsigned long function_id,
469695
unsigned long arg0, unsigned long arg1,
470696
unsigned long arg2, unsigned long arg3,
@@ -547,9 +773,36 @@ static int sdei_probe(struct platform_device *pdev)
547773
return 0;
548774
}
549775

550-
on_each_cpu(&_ipi_unmask_cpu, NULL, false);
776+
err = cpu_pm_register_notifier(&sdei_pm_nb);
777+
if (err) {
778+
pr_warn("Failed to register CPU PM notifier...\n");
779+
goto error;
780+
}
781+
782+
err = register_reboot_notifier(&sdei_reboot_nb);
783+
if (err) {
784+
pr_warn("Failed to register reboot notifier...\n");
785+
goto remove_cpupm;
786+
}
787+
788+
err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI",
789+
&sdei_cpuhp_up, &sdei_cpuhp_down);
790+
if (err) {
791+
pr_warn("Failed to register CPU hotplug notifier...\n");
792+
goto remove_reboot;
793+
}
551794

552795
return 0;
796+
797+
remove_reboot:
798+
unregister_reboot_notifier(&sdei_reboot_nb);
799+
800+
remove_cpupm:
801+
cpu_pm_unregister_notifier(&sdei_pm_nb);
802+
803+
error:
804+
sdei_mark_interface_broken();
805+
return err;
553806
}
554807

555808
static const struct of_device_id sdei_of_match[] = {
@@ -560,6 +813,7 @@ static const struct of_device_id sdei_of_match[] = {
560813
static struct platform_driver sdei_driver = {
561814
.driver = {
562815
.name = "sdei",
816+
.pm = &sdei_pm_ops,
563817
.of_match_table = sdei_of_match,
564818
},
565819
.probe = sdei_probe,

‎include/linux/cpuhotplug.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ enum cpuhp_state {
109109
CPUHP_AP_PERF_XTENSA_STARTING,
110110
CPUHP_AP_PERF_METAG_STARTING,
111111
CPUHP_AP_MIPS_OP_LOONGSON3_STARTING,
112+
CPUHP_AP_ARM_SDEI_STARTING,
112113
CPUHP_AP_ARM_VFP_STARTING,
113114
CPUHP_AP_ARM64_DEBUG_MONITORS_STARTING,
114115
CPUHP_AP_PERF_ARM_HW_BREAKPOINT_STARTING,

0 commit comments

Comments
 (0)