mpls: Return error for RTA_GATEWAY attribute
[linux-stable.git] / drivers / firmware / arm_sdei.c
blobc64c7da738297453ee05f173c1a6c0d4c0fadbe9
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2017 Arm Ltd.
3 #define pr_fmt(fmt) "sdei: " fmt
5 #include <linux/acpi.h>
6 #include <linux/arm_sdei.h>
7 #include <linux/arm-smccc.h>
8 #include <linux/atomic.h>
9 #include <linux/bitops.h>
10 #include <linux/compiler.h>
11 #include <linux/cpuhotplug.h>
12 #include <linux/cpu.h>
13 #include <linux/cpu_pm.h>
14 #include <linux/errno.h>
15 #include <linux/hardirq.h>
16 #include <linux/kernel.h>
17 #include <linux/kprobes.h>
18 #include <linux/kvm_host.h>
19 #include <linux/list.h>
20 #include <linux/mutex.h>
21 #include <linux/notifier.h>
22 #include <linux/of.h>
23 #include <linux/of_platform.h>
24 #include <linux/percpu.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm.h>
27 #include <linux/ptrace.h>
28 #include <linux/preempt.h>
29 #include <linux/reboot.h>
30 #include <linux/slab.h>
31 #include <linux/smp.h>
32 #include <linux/spinlock.h>
33 #include <linux/uaccess.h>
36 * The call to use to reach the firmware.
38 static asmlinkage void (*sdei_firmware_call)(unsigned long function_id,
39 unsigned long arg0, unsigned long arg1,
40 unsigned long arg2, unsigned long arg3,
41 unsigned long arg4, struct arm_smccc_res *res);
43 /* entry point from firmware to arch asm code */
44 static unsigned long sdei_entry_point;
46 struct sdei_event {
47 /* These three are protected by the sdei_list_lock */
48 struct list_head list;
49 bool reregister;
50 bool reenable;
52 u32 event_num;
53 u8 type;
54 u8 priority;
56 /* This pointer is handed to firmware as the event argument. */
57 union {
58 /* Shared events */
59 struct sdei_registered_event *registered;
61 /* CPU private events */
62 struct sdei_registered_event __percpu *private_registered;
66 /* Take the mutex for any API call or modification. Take the mutex first. */
67 static DEFINE_MUTEX(sdei_events_lock);
69 /* and then hold this when modifying the list */
70 static DEFINE_SPINLOCK(sdei_list_lock);
71 static LIST_HEAD(sdei_list);
73 /* Private events are registered/enabled via IPI passing one of these */
74 struct sdei_crosscall_args {
75 struct sdei_event *event;
76 atomic_t errors;
77 int first_error;
80 #define CROSSCALL_INIT(arg, event) (arg.event = event, \
81 arg.first_error = 0, \
82 atomic_set(&arg.errors, 0))
84 static inline int sdei_do_cross_call(void *fn, struct sdei_event * event)
86 struct sdei_crosscall_args arg;
88 CROSSCALL_INIT(arg, event);
89 on_each_cpu(fn, &arg, true);
91 return arg.first_error;
94 static inline void
95 sdei_cross_call_return(struct sdei_crosscall_args *arg, int err)
97 if (err && (atomic_inc_return(&arg->errors) == 1))
98 arg->first_error = err;
101 static int sdei_to_linux_errno(unsigned long sdei_err)
103 switch (sdei_err) {
104 case SDEI_NOT_SUPPORTED:
105 return -EOPNOTSUPP;
106 case SDEI_INVALID_PARAMETERS:
107 return -EINVAL;
108 case SDEI_DENIED:
109 return -EPERM;
110 case SDEI_PENDING:
111 return -EINPROGRESS;
112 case SDEI_OUT_OF_RESOURCE:
113 return -ENOMEM;
116 /* Not an error value ... */
117 return sdei_err;
121 * If x0 is any of these values, then the call failed, use sdei_to_linux_errno()
122 * to translate.
124 static int sdei_is_err(struct arm_smccc_res *res)
126 switch (res->a0) {
127 case SDEI_NOT_SUPPORTED:
128 case SDEI_INVALID_PARAMETERS:
129 case SDEI_DENIED:
130 case SDEI_PENDING:
131 case SDEI_OUT_OF_RESOURCE:
132 return true;
135 return false;
138 static int invoke_sdei_fn(unsigned long function_id, unsigned long arg0,
139 unsigned long arg1, unsigned long arg2,
140 unsigned long arg3, unsigned long arg4,
141 u64 *result)
143 int err = 0;
144 struct arm_smccc_res res;
146 if (sdei_firmware_call) {
147 sdei_firmware_call(function_id, arg0, arg1, arg2, arg3, arg4,
148 &res);
149 if (sdei_is_err(&res))
150 err = sdei_to_linux_errno(res.a0);
151 } else {
153 * !sdei_firmware_call means we failed to probe or called
154 * sdei_mark_interface_broken(). -EIO is not an error returned
155 * by sdei_to_linux_errno() and is used to suppress messages
156 * from this driver.
158 err = -EIO;
159 res.a0 = SDEI_NOT_SUPPORTED;
162 if (result)
163 *result = res.a0;
165 return err;
168 static struct sdei_event *sdei_event_find(u32 event_num)
170 struct sdei_event *e, *found = NULL;
172 lockdep_assert_held(&sdei_events_lock);
174 spin_lock(&sdei_list_lock);
175 list_for_each_entry(e, &sdei_list, list) {
176 if (e->event_num == event_num) {
177 found = e;
178 break;
181 spin_unlock(&sdei_list_lock);
183 return found;
186 int sdei_api_event_context(u32 query, u64 *result)
188 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, query, 0, 0, 0, 0,
189 result);
191 NOKPROBE_SYMBOL(sdei_api_event_context);
193 static int sdei_api_event_get_info(u32 event, u32 info, u64 *result)
195 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_GET_INFO, event, info, 0,
196 0, 0, result);
199 static struct sdei_event *sdei_event_create(u32 event_num,
200 sdei_event_callback *cb,
201 void *cb_arg)
203 int err;
204 u64 result;
205 struct sdei_event *event;
206 struct sdei_registered_event *reg;
208 lockdep_assert_held(&sdei_events_lock);
210 event = kzalloc(sizeof(*event), GFP_KERNEL);
211 if (!event)
212 return ERR_PTR(-ENOMEM);
214 INIT_LIST_HEAD(&event->list);
215 event->event_num = event_num;
217 err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY,
218 &result);
219 if (err) {
220 kfree(event);
221 return ERR_PTR(err);
223 event->priority = result;
225 err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_TYPE,
226 &result);
227 if (err) {
228 kfree(event);
229 return ERR_PTR(err);
231 event->type = result;
233 if (event->type == SDEI_EVENT_TYPE_SHARED) {
234 reg = kzalloc(sizeof(*reg), GFP_KERNEL);
235 if (!reg) {
236 kfree(event);
237 return ERR_PTR(-ENOMEM);
240 reg->event_num = event_num;
241 reg->priority = event->priority;
243 reg->callback = cb;
244 reg->callback_arg = cb_arg;
245 event->registered = reg;
246 } else {
247 int cpu;
248 struct sdei_registered_event __percpu *regs;
250 regs = alloc_percpu(struct sdei_registered_event);
251 if (!regs) {
252 kfree(event);
253 return ERR_PTR(-ENOMEM);
256 for_each_possible_cpu(cpu) {
257 reg = per_cpu_ptr(regs, cpu);
259 reg->event_num = event->event_num;
260 reg->priority = event->priority;
261 reg->callback = cb;
262 reg->callback_arg = cb_arg;
265 event->private_registered = regs;
268 if (sdei_event_find(event_num)) {
269 kfree(event->registered);
270 kfree(event);
271 event = ERR_PTR(-EBUSY);
272 } else {
273 spin_lock(&sdei_list_lock);
274 list_add(&event->list, &sdei_list);
275 spin_unlock(&sdei_list_lock);
278 return event;
281 static void sdei_event_destroy(struct sdei_event *event)
283 lockdep_assert_held(&sdei_events_lock);
285 spin_lock(&sdei_list_lock);
286 list_del(&event->list);
287 spin_unlock(&sdei_list_lock);
289 if (event->type == SDEI_EVENT_TYPE_SHARED)
290 kfree(event->registered);
291 else
292 free_percpu(event->private_registered);
294 kfree(event);
297 static int sdei_api_get_version(u64 *version)
299 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_VERSION, 0, 0, 0, 0, 0, version);
302 int sdei_mask_local_cpu(void)
304 int err;
306 WARN_ON_ONCE(preemptible());
308 err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_MASK, 0, 0, 0, 0, 0, NULL);
309 if (err && err != -EIO) {
310 pr_warn_once("failed to mask CPU[%u]: %d\n",
311 smp_processor_id(), err);
312 return err;
315 return 0;
318 static void _ipi_mask_cpu(void *ignored)
320 sdei_mask_local_cpu();
323 int sdei_unmask_local_cpu(void)
325 int err;
327 WARN_ON_ONCE(preemptible());
329 err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_UNMASK, 0, 0, 0, 0, 0, NULL);
330 if (err && err != -EIO) {
331 pr_warn_once("failed to unmask CPU[%u]: %d\n",
332 smp_processor_id(), err);
333 return err;
336 return 0;
339 static void _ipi_unmask_cpu(void *ignored)
341 sdei_unmask_local_cpu();
344 static void _ipi_private_reset(void *ignored)
346 int err;
348 err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PRIVATE_RESET, 0, 0, 0, 0, 0,
349 NULL);
350 if (err && err != -EIO)
351 pr_warn_once("failed to reset CPU[%u]: %d\n",
352 smp_processor_id(), err);
355 static int sdei_api_shared_reset(void)
357 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_SHARED_RESET, 0, 0, 0, 0, 0,
358 NULL);
361 static void sdei_mark_interface_broken(void)
363 pr_err("disabling SDEI firmware interface\n");
364 on_each_cpu(&_ipi_mask_cpu, NULL, true);
365 sdei_firmware_call = NULL;
368 static int sdei_platform_reset(void)
370 int err;
372 on_each_cpu(&_ipi_private_reset, NULL, true);
373 err = sdei_api_shared_reset();
374 if (err) {
375 pr_err("Failed to reset platform: %d\n", err);
376 sdei_mark_interface_broken();
379 return err;
382 static int sdei_api_event_enable(u32 event_num)
384 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_ENABLE, event_num, 0, 0, 0,
385 0, NULL);
388 /* Called directly by the hotplug callbacks */
389 static void _local_event_enable(void *data)
391 int err;
392 struct sdei_crosscall_args *arg = data;
394 WARN_ON_ONCE(preemptible());
396 err = sdei_api_event_enable(arg->event->event_num);
398 sdei_cross_call_return(arg, err);
401 int sdei_event_enable(u32 event_num)
403 int err = -EINVAL;
404 struct sdei_event *event;
406 mutex_lock(&sdei_events_lock);
407 event = sdei_event_find(event_num);
408 if (!event) {
409 mutex_unlock(&sdei_events_lock);
410 return -ENOENT;
413 spin_lock(&sdei_list_lock);
414 event->reenable = true;
415 spin_unlock(&sdei_list_lock);
417 if (event->type == SDEI_EVENT_TYPE_SHARED)
418 err = sdei_api_event_enable(event->event_num);
419 else
420 err = sdei_do_cross_call(_local_event_enable, event);
421 mutex_unlock(&sdei_events_lock);
423 return err;
425 EXPORT_SYMBOL(sdei_event_enable);
427 static int sdei_api_event_disable(u32 event_num)
429 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_DISABLE, event_num, 0, 0,
430 0, 0, NULL);
433 static void _ipi_event_disable(void *data)
435 int err;
436 struct sdei_crosscall_args *arg = data;
438 err = sdei_api_event_disable(arg->event->event_num);
440 sdei_cross_call_return(arg, err);
443 int sdei_event_disable(u32 event_num)
445 int err = -EINVAL;
446 struct sdei_event *event;
448 mutex_lock(&sdei_events_lock);
449 event = sdei_event_find(event_num);
450 if (!event) {
451 mutex_unlock(&sdei_events_lock);
452 return -ENOENT;
455 spin_lock(&sdei_list_lock);
456 event->reenable = false;
457 spin_unlock(&sdei_list_lock);
459 if (event->type == SDEI_EVENT_TYPE_SHARED)
460 err = sdei_api_event_disable(event->event_num);
461 else
462 err = sdei_do_cross_call(_ipi_event_disable, event);
463 mutex_unlock(&sdei_events_lock);
465 return err;
467 EXPORT_SYMBOL(sdei_event_disable);
469 static int sdei_api_event_unregister(u32 event_num)
471 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_UNREGISTER, event_num, 0,
472 0, 0, 0, NULL);
475 /* Called directly by the hotplug callbacks */
476 static void _local_event_unregister(void *data)
478 int err;
479 struct sdei_crosscall_args *arg = data;
481 WARN_ON_ONCE(preemptible());
483 err = sdei_api_event_unregister(arg->event->event_num);
485 sdei_cross_call_return(arg, err);
488 static int _sdei_event_unregister(struct sdei_event *event)
490 lockdep_assert_held(&sdei_events_lock);
492 spin_lock(&sdei_list_lock);
493 event->reregister = false;
494 event->reenable = false;
495 spin_unlock(&sdei_list_lock);
497 if (event->type == SDEI_EVENT_TYPE_SHARED)
498 return sdei_api_event_unregister(event->event_num);
500 return sdei_do_cross_call(_local_event_unregister, event);
503 int sdei_event_unregister(u32 event_num)
505 int err;
506 struct sdei_event *event;
508 WARN_ON(in_nmi());
510 mutex_lock(&sdei_events_lock);
511 event = sdei_event_find(event_num);
512 do {
513 if (!event) {
514 pr_warn("Event %u not registered\n", event_num);
515 err = -ENOENT;
516 break;
519 err = _sdei_event_unregister(event);
520 if (err)
521 break;
523 sdei_event_destroy(event);
524 } while (0);
525 mutex_unlock(&sdei_events_lock);
527 return err;
529 EXPORT_SYMBOL(sdei_event_unregister);
532 * unregister events, but don't destroy them as they are re-registered by
533 * sdei_reregister_shared().
535 static int sdei_unregister_shared(void)
537 int err = 0;
538 struct sdei_event *event;
540 mutex_lock(&sdei_events_lock);
541 spin_lock(&sdei_list_lock);
542 list_for_each_entry(event, &sdei_list, list) {
543 if (event->type != SDEI_EVENT_TYPE_SHARED)
544 continue;
546 err = _sdei_event_unregister(event);
547 if (err)
548 break;
550 spin_unlock(&sdei_list_lock);
551 mutex_unlock(&sdei_events_lock);
553 return err;
556 static int sdei_api_event_register(u32 event_num, unsigned long entry_point,
557 void *arg, u64 flags, u64 affinity)
559 return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_REGISTER, event_num,
560 (unsigned long)entry_point, (unsigned long)arg,
561 flags, affinity, NULL);
564 /* Called directly by the hotplug callbacks */
565 static void _local_event_register(void *data)
567 int err;
568 struct sdei_registered_event *reg;
569 struct sdei_crosscall_args *arg = data;
571 WARN_ON(preemptible());
573 reg = per_cpu_ptr(arg->event->private_registered, smp_processor_id());
574 err = sdei_api_event_register(arg->event->event_num, sdei_entry_point,
575 reg, 0, 0);
577 sdei_cross_call_return(arg, err);
580 static int _sdei_event_register(struct sdei_event *event)
582 int err;
584 lockdep_assert_held(&sdei_events_lock);
586 spin_lock(&sdei_list_lock);
587 event->reregister = true;
588 spin_unlock(&sdei_list_lock);
590 if (event->type == SDEI_EVENT_TYPE_SHARED)
591 return sdei_api_event_register(event->event_num,
592 sdei_entry_point,
593 event->registered,
594 SDEI_EVENT_REGISTER_RM_ANY, 0);
597 err = sdei_do_cross_call(_local_event_register, event);
598 if (err) {
599 spin_lock(&sdei_list_lock);
600 event->reregister = false;
601 event->reenable = false;
602 spin_unlock(&sdei_list_lock);
604 sdei_do_cross_call(_local_event_unregister, event);
607 return err;
610 int sdei_event_register(u32 event_num, sdei_event_callback *cb, void *arg)
612 int err;
613 struct sdei_event *event;
615 WARN_ON(in_nmi());
617 mutex_lock(&sdei_events_lock);
618 do {
619 if (sdei_event_find(event_num)) {
620 pr_warn("Event %u already registered\n", event_num);
621 err = -EBUSY;
622 break;
625 event = sdei_event_create(event_num, cb, arg);
626 if (IS_ERR(event)) {
627 err = PTR_ERR(event);
628 pr_warn("Failed to create event %u: %d\n", event_num,
629 err);
630 break;
633 err = _sdei_event_register(event);
634 if (err) {
635 sdei_event_destroy(event);
636 pr_warn("Failed to register event %u: %d\n", event_num,
637 err);
639 } while (0);
640 mutex_unlock(&sdei_events_lock);
642 return err;
644 EXPORT_SYMBOL(sdei_event_register);
646 static int sdei_reregister_event(struct sdei_event *event)
648 int err;
650 lockdep_assert_held(&sdei_events_lock);
652 err = _sdei_event_register(event);
653 if (err) {
654 pr_err("Failed to re-register event %u\n", event->event_num);
655 sdei_event_destroy(event);
656 return err;
659 if (event->reenable) {
660 if (event->type == SDEI_EVENT_TYPE_SHARED)
661 err = sdei_api_event_enable(event->event_num);
662 else
663 err = sdei_do_cross_call(_local_event_enable, event);
666 if (err)
667 pr_err("Failed to re-enable event %u\n", event->event_num);
669 return err;
672 static int sdei_reregister_shared(void)
674 int err = 0;
675 struct sdei_event *event;
677 mutex_lock(&sdei_events_lock);
678 spin_lock(&sdei_list_lock);
679 list_for_each_entry(event, &sdei_list, list) {
680 if (event->type != SDEI_EVENT_TYPE_SHARED)
681 continue;
683 if (event->reregister) {
684 err = sdei_reregister_event(event);
685 if (err)
686 break;
689 spin_unlock(&sdei_list_lock);
690 mutex_unlock(&sdei_events_lock);
692 return err;
695 static int sdei_cpuhp_down(unsigned int cpu)
697 struct sdei_event *event;
698 struct sdei_crosscall_args arg;
700 /* un-register private events */
701 spin_lock(&sdei_list_lock);
702 list_for_each_entry(event, &sdei_list, list) {
703 if (event->type == SDEI_EVENT_TYPE_SHARED)
704 continue;
706 CROSSCALL_INIT(arg, event);
707 /* call the cross-call function locally... */
708 _local_event_unregister(&arg);
709 if (arg.first_error)
710 pr_err("Failed to unregister event %u: %d\n",
711 event->event_num, arg.first_error);
713 spin_unlock(&sdei_list_lock);
715 return sdei_mask_local_cpu();
718 static int sdei_cpuhp_up(unsigned int cpu)
720 struct sdei_event *event;
721 struct sdei_crosscall_args arg;
723 /* re-register/enable private events */
724 spin_lock(&sdei_list_lock);
725 list_for_each_entry(event, &sdei_list, list) {
726 if (event->type == SDEI_EVENT_TYPE_SHARED)
727 continue;
729 if (event->reregister) {
730 CROSSCALL_INIT(arg, event);
731 /* call the cross-call function locally... */
732 _local_event_register(&arg);
733 if (arg.first_error)
734 pr_err("Failed to re-register event %u: %d\n",
735 event->event_num, arg.first_error);
738 if (event->reenable) {
739 CROSSCALL_INIT(arg, event);
740 _local_event_enable(&arg);
741 if (arg.first_error)
742 pr_err("Failed to re-enable event %u: %d\n",
743 event->event_num, arg.first_error);
746 spin_unlock(&sdei_list_lock);
748 return sdei_unmask_local_cpu();
751 /* When entering idle, mask/unmask events for this cpu */
752 static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action,
753 void *data)
755 int rv;
757 switch (action) {
758 case CPU_PM_ENTER:
759 rv = sdei_mask_local_cpu();
760 break;
761 case CPU_PM_EXIT:
762 case CPU_PM_ENTER_FAILED:
763 rv = sdei_unmask_local_cpu();
764 break;
765 default:
766 return NOTIFY_DONE;
769 if (rv)
770 return notifier_from_errno(rv);
772 return NOTIFY_OK;
775 static struct notifier_block sdei_pm_nb = {
776 .notifier_call = sdei_pm_notifier,
779 static int sdei_device_suspend(struct device *dev)
781 on_each_cpu(_ipi_mask_cpu, NULL, true);
783 return 0;
786 static int sdei_device_resume(struct device *dev)
788 on_each_cpu(_ipi_unmask_cpu, NULL, true);
790 return 0;
794 * We need all events to be reregistered when we resume from hibernate.
796 * The sequence is freeze->thaw. Reboot. freeze->restore. We unregister
797 * events during freeze, then re-register and re-enable them during thaw
798 * and restore.
800 static int sdei_device_freeze(struct device *dev)
802 int err;
804 /* unregister private events */
805 cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING);
807 err = sdei_unregister_shared();
808 if (err)
809 return err;
811 return 0;
814 static int sdei_device_thaw(struct device *dev)
816 int err;
818 /* re-register shared events */
819 err = sdei_reregister_shared();
820 if (err) {
821 pr_warn("Failed to re-register shared events...\n");
822 sdei_mark_interface_broken();
823 return err;
826 err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI",
827 &sdei_cpuhp_up, &sdei_cpuhp_down);
828 if (err)
829 pr_warn("Failed to re-register CPU hotplug notifier...\n");
831 return err;
834 static int sdei_device_restore(struct device *dev)
836 int err;
838 err = sdei_platform_reset();
839 if (err)
840 return err;
842 return sdei_device_thaw(dev);
845 static const struct dev_pm_ops sdei_pm_ops = {
846 .suspend = sdei_device_suspend,
847 .resume = sdei_device_resume,
848 .freeze = sdei_device_freeze,
849 .thaw = sdei_device_thaw,
850 .restore = sdei_device_restore,
854 * Mask all CPUs and unregister all events on panic, reboot or kexec.
856 static int sdei_reboot_notifier(struct notifier_block *nb, unsigned long action,
857 void *data)
860 * We are going to reset the interface, after this there is no point
861 * doing work when we take CPUs offline.
863 cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING);
865 sdei_platform_reset();
867 return NOTIFY_OK;
870 static struct notifier_block sdei_reboot_nb = {
871 .notifier_call = sdei_reboot_notifier,
874 static void sdei_smccc_smc(unsigned long function_id,
875 unsigned long arg0, unsigned long arg1,
876 unsigned long arg2, unsigned long arg3,
877 unsigned long arg4, struct arm_smccc_res *res)
879 arm_smccc_smc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res);
882 static void sdei_smccc_hvc(unsigned long function_id,
883 unsigned long arg0, unsigned long arg1,
884 unsigned long arg2, unsigned long arg3,
885 unsigned long arg4, struct arm_smccc_res *res)
887 arm_smccc_hvc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res);
890 static int sdei_get_conduit(struct platform_device *pdev)
892 const char *method;
893 struct device_node *np = pdev->dev.of_node;
895 sdei_firmware_call = NULL;
896 if (np) {
897 if (of_property_read_string(np, "method", &method)) {
898 pr_warn("missing \"method\" property\n");
899 return CONDUIT_INVALID;
902 if (!strcmp("hvc", method)) {
903 sdei_firmware_call = &sdei_smccc_hvc;
904 return CONDUIT_HVC;
905 } else if (!strcmp("smc", method)) {
906 sdei_firmware_call = &sdei_smccc_smc;
907 return CONDUIT_SMC;
910 pr_warn("invalid \"method\" property: %s\n", method);
911 } else if (IS_ENABLED(CONFIG_ACPI) && !acpi_disabled) {
912 if (acpi_psci_use_hvc()) {
913 sdei_firmware_call = &sdei_smccc_hvc;
914 return CONDUIT_HVC;
915 } else {
916 sdei_firmware_call = &sdei_smccc_smc;
917 return CONDUIT_SMC;
921 return CONDUIT_INVALID;
924 static int sdei_probe(struct platform_device *pdev)
926 int err;
927 u64 ver = 0;
928 int conduit;
930 conduit = sdei_get_conduit(pdev);
931 if (!sdei_firmware_call)
932 return 0;
934 err = sdei_api_get_version(&ver);
935 if (err == -EOPNOTSUPP)
936 pr_err("advertised but not implemented in platform firmware\n");
937 if (err) {
938 pr_err("Failed to get SDEI version: %d\n", err);
939 sdei_mark_interface_broken();
940 return err;
943 pr_info("SDEIv%d.%d (0x%x) detected in firmware.\n",
944 (int)SDEI_VERSION_MAJOR(ver), (int)SDEI_VERSION_MINOR(ver),
945 (int)SDEI_VERSION_VENDOR(ver));
947 if (SDEI_VERSION_MAJOR(ver) != 1) {
948 pr_warn("Conflicting SDEI version detected.\n");
949 sdei_mark_interface_broken();
950 return -EINVAL;
953 err = sdei_platform_reset();
954 if (err)
955 return err;
957 sdei_entry_point = sdei_arch_get_entry_point(conduit);
958 if (!sdei_entry_point) {
959 /* Not supported due to hardware or boot configuration */
960 sdei_mark_interface_broken();
961 return 0;
964 err = cpu_pm_register_notifier(&sdei_pm_nb);
965 if (err) {
966 pr_warn("Failed to register CPU PM notifier...\n");
967 goto error;
970 err = register_reboot_notifier(&sdei_reboot_nb);
971 if (err) {
972 pr_warn("Failed to register reboot notifier...\n");
973 goto remove_cpupm;
976 err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI",
977 &sdei_cpuhp_up, &sdei_cpuhp_down);
978 if (err) {
979 pr_warn("Failed to register CPU hotplug notifier...\n");
980 goto remove_reboot;
983 return 0;
985 remove_reboot:
986 unregister_reboot_notifier(&sdei_reboot_nb);
988 remove_cpupm:
989 cpu_pm_unregister_notifier(&sdei_pm_nb);
991 error:
992 sdei_mark_interface_broken();
993 return err;
996 static const struct of_device_id sdei_of_match[] = {
997 { .compatible = "arm,sdei-1.0" },
1001 static struct platform_driver sdei_driver = {
1002 .driver = {
1003 .name = "sdei",
1004 .pm = &sdei_pm_ops,
1005 .of_match_table = sdei_of_match,
1007 .probe = sdei_probe,
1010 static bool __init sdei_present_dt(void)
1012 struct device_node *np, *fw_np;
1014 fw_np = of_find_node_by_name(NULL, "firmware");
1015 if (!fw_np)
1016 return false;
1018 np = of_find_matching_node(fw_np, sdei_of_match);
1019 if (!np)
1020 return false;
1021 of_node_put(np);
1023 return true;
1026 static bool __init sdei_present_acpi(void)
1028 acpi_status status;
1029 struct platform_device *pdev;
1030 struct acpi_table_header *sdei_table_header;
1032 if (acpi_disabled)
1033 return false;
1035 status = acpi_get_table(ACPI_SIG_SDEI, 0, &sdei_table_header);
1036 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
1037 const char *msg = acpi_format_exception(status);
1039 pr_info("Failed to get ACPI:SDEI table, %s\n", msg);
1041 if (ACPI_FAILURE(status))
1042 return false;
1044 pdev = platform_device_register_simple(sdei_driver.driver.name, 0, NULL,
1046 if (IS_ERR(pdev))
1047 return false;
1049 return true;
1052 static int __init sdei_init(void)
1054 if (sdei_present_dt() || sdei_present_acpi())
1055 platform_driver_register(&sdei_driver);
1057 return 0;
1061 * On an ACPI system SDEI needs to be ready before HEST:GHES tries to register
1062 * its events. ACPI is initialised from a subsys_initcall(), GHES is initialised
1063 * by device_initcall(). We want to be called in the middle.
1065 subsys_initcall_sync(sdei_init);
1067 int sdei_event_handler(struct pt_regs *regs,
1068 struct sdei_registered_event *arg)
1070 int err;
1071 mm_segment_t orig_addr_limit;
1072 u32 event_num = arg->event_num;
1074 orig_addr_limit = get_fs();
1075 set_fs(USER_DS);
1077 err = arg->callback(event_num, regs, arg->callback_arg);
1078 if (err)
1079 pr_err_ratelimited("event %u on CPU %u failed with error: %d\n",
1080 event_num, smp_processor_id(), err);
1082 set_fs(orig_addr_limit);
1084 return err;
1086 NOKPROBE_SYMBOL(sdei_event_handler);