1ad6eb31eSJames Morse // SPDX-License-Identifier: GPL-2.0 2ad6eb31eSJames Morse // Copyright (C) 2017 Arm Ltd. 3ad6eb31eSJames Morse #define pr_fmt(fmt) "sdei: " fmt 4ad6eb31eSJames Morse 5f96935d3SJames Morse #include <acpi/ghes.h> 6ad6eb31eSJames Morse #include <linux/acpi.h> 7ad6eb31eSJames Morse #include <linux/arm_sdei.h> 8ad6eb31eSJames Morse #include <linux/arm-smccc.h> 9f92b5462SJames Morse #include <linux/atomic.h> 10ad6eb31eSJames Morse #include <linux/bitops.h> 11ad6eb31eSJames Morse #include <linux/compiler.h> 12da351827SJames Morse #include <linux/cpuhotplug.h> 13f92b5462SJames Morse #include <linux/cpu.h> 14da351827SJames Morse #include <linux/cpu_pm.h> 15ad6eb31eSJames Morse #include <linux/errno.h> 16ad6eb31eSJames Morse #include <linux/hardirq.h> 17ad6eb31eSJames Morse #include <linux/kernel.h> 18ad6eb31eSJames Morse #include <linux/kprobes.h> 19ad6eb31eSJames Morse #include <linux/kvm_host.h> 20ad6eb31eSJames Morse #include <linux/list.h> 21ad6eb31eSJames Morse #include <linux/mutex.h> 22da351827SJames Morse #include <linux/notifier.h> 23ad6eb31eSJames Morse #include <linux/of.h> 24ad6eb31eSJames Morse #include <linux/of_platform.h> 25ad6eb31eSJames Morse #include <linux/percpu.h> 26ad6eb31eSJames Morse #include <linux/platform_device.h> 27da351827SJames Morse #include <linux/pm.h> 28ad6eb31eSJames Morse #include <linux/ptrace.h> 29ad6eb31eSJames Morse #include <linux/preempt.h> 30da351827SJames Morse #include <linux/reboot.h> 31ad6eb31eSJames Morse #include <linux/slab.h> 32ad6eb31eSJames Morse #include <linux/smp.h> 33ad6eb31eSJames Morse #include <linux/spinlock.h> 34ad6eb31eSJames Morse #include <linux/uaccess.h> 35ad6eb31eSJames Morse 36ad6eb31eSJames Morse /* 37ad6eb31eSJames Morse * The call to use to reach the firmware. 38ad6eb31eSJames Morse */ 39ad6eb31eSJames Morse static asmlinkage void (*sdei_firmware_call)(unsigned long function_id, 40ad6eb31eSJames Morse unsigned long arg0, unsigned long arg1, 41ad6eb31eSJames Morse unsigned long arg2, unsigned long arg3, 42ad6eb31eSJames Morse unsigned long arg4, struct arm_smccc_res *res); 43ad6eb31eSJames Morse 44ad6eb31eSJames Morse /* entry point from firmware to arch asm code */ 45ad6eb31eSJames Morse static unsigned long sdei_entry_point; 46ad6eb31eSJames Morse 47ad6eb31eSJames Morse struct sdei_event { 48da351827SJames Morse /* These three are protected by the sdei_list_lock */ 49ad6eb31eSJames Morse struct list_head list; 50da351827SJames Morse bool reregister; 51da351827SJames Morse bool reenable; 52da351827SJames Morse 53ad6eb31eSJames Morse u32 event_num; 54ad6eb31eSJames Morse u8 type; 55ad6eb31eSJames Morse u8 priority; 56ad6eb31eSJames Morse 57ad6eb31eSJames Morse /* This pointer is handed to firmware as the event argument. */ 58f92b5462SJames Morse union { 59f92b5462SJames Morse /* Shared events */ 60ad6eb31eSJames Morse struct sdei_registered_event *registered; 61f92b5462SJames Morse 62f92b5462SJames Morse /* CPU private events */ 63f92b5462SJames Morse struct sdei_registered_event __percpu *private_registered; 64f92b5462SJames Morse }; 65ad6eb31eSJames Morse }; 66ad6eb31eSJames Morse 67ad6eb31eSJames Morse /* Take the mutex for any API call or modification. Take the mutex first. */ 68ad6eb31eSJames Morse static DEFINE_MUTEX(sdei_events_lock); 69ad6eb31eSJames Morse 70ad6eb31eSJames Morse /* and then hold this when modifying the list */ 71ad6eb31eSJames Morse static DEFINE_SPINLOCK(sdei_list_lock); 72ad6eb31eSJames Morse static LIST_HEAD(sdei_list); 73ad6eb31eSJames Morse 74f92b5462SJames Morse /* Private events are registered/enabled via IPI passing one of these */ 75f92b5462SJames Morse struct sdei_crosscall_args { 76f92b5462SJames Morse struct sdei_event *event; 77f92b5462SJames Morse atomic_t errors; 78f92b5462SJames Morse int first_error; 79f92b5462SJames Morse }; 80f92b5462SJames Morse 81f92b5462SJames Morse #define CROSSCALL_INIT(arg, event) (arg.event = event, \ 82f92b5462SJames Morse arg.first_error = 0, \ 83f92b5462SJames Morse atomic_set(&arg.errors, 0)) 84f92b5462SJames Morse 85f92b5462SJames Morse static inline int sdei_do_cross_call(void *fn, struct sdei_event * event) 86f92b5462SJames Morse { 87f92b5462SJames Morse struct sdei_crosscall_args arg; 88f92b5462SJames Morse 89f92b5462SJames Morse CROSSCALL_INIT(arg, event); 90f92b5462SJames Morse on_each_cpu(fn, &arg, true); 91f92b5462SJames Morse 92f92b5462SJames Morse return arg.first_error; 93f92b5462SJames Morse } 94f92b5462SJames Morse 95f92b5462SJames Morse static inline void 96f92b5462SJames Morse sdei_cross_call_return(struct sdei_crosscall_args *arg, int err) 97f92b5462SJames Morse { 98f92b5462SJames Morse if (err && (atomic_inc_return(&arg->errors) == 1)) 99f92b5462SJames Morse arg->first_error = err; 100f92b5462SJames Morse } 101f92b5462SJames Morse 102ad6eb31eSJames Morse static int sdei_to_linux_errno(unsigned long sdei_err) 103ad6eb31eSJames Morse { 104ad6eb31eSJames Morse switch (sdei_err) { 105ad6eb31eSJames Morse case SDEI_NOT_SUPPORTED: 106ad6eb31eSJames Morse return -EOPNOTSUPP; 107ad6eb31eSJames Morse case SDEI_INVALID_PARAMETERS: 108ad6eb31eSJames Morse return -EINVAL; 109ad6eb31eSJames Morse case SDEI_DENIED: 110ad6eb31eSJames Morse return -EPERM; 111ad6eb31eSJames Morse case SDEI_PENDING: 112ad6eb31eSJames Morse return -EINPROGRESS; 113ad6eb31eSJames Morse case SDEI_OUT_OF_RESOURCE: 114ad6eb31eSJames Morse return -ENOMEM; 115ad6eb31eSJames Morse } 116ad6eb31eSJames Morse 1175735f515SGavin Shan return 0; 118ad6eb31eSJames Morse } 119ad6eb31eSJames Morse 120ad6eb31eSJames Morse static int invoke_sdei_fn(unsigned long function_id, unsigned long arg0, 121ad6eb31eSJames Morse unsigned long arg1, unsigned long arg2, 122ad6eb31eSJames Morse unsigned long arg3, unsigned long arg4, 123ad6eb31eSJames Morse u64 *result) 124ad6eb31eSJames Morse { 1255735f515SGavin Shan int err; 126ad6eb31eSJames Morse struct arm_smccc_res res; 127ad6eb31eSJames Morse 128ad6eb31eSJames Morse if (sdei_firmware_call) { 129ad6eb31eSJames Morse sdei_firmware_call(function_id, arg0, arg1, arg2, arg3, arg4, 130ad6eb31eSJames Morse &res); 131ad6eb31eSJames Morse err = sdei_to_linux_errno(res.a0); 132ad6eb31eSJames Morse } else { 133ad6eb31eSJames Morse /* 134ad6eb31eSJames Morse * !sdei_firmware_call means we failed to probe or called 135ad6eb31eSJames Morse * sdei_mark_interface_broken(). -EIO is not an error returned 136ad6eb31eSJames Morse * by sdei_to_linux_errno() and is used to suppress messages 137ad6eb31eSJames Morse * from this driver. 138ad6eb31eSJames Morse */ 139ad6eb31eSJames Morse err = -EIO; 140ad6eb31eSJames Morse res.a0 = SDEI_NOT_SUPPORTED; 141ad6eb31eSJames Morse } 142ad6eb31eSJames Morse 143ad6eb31eSJames Morse if (result) 144ad6eb31eSJames Morse *result = res.a0; 145ad6eb31eSJames Morse 146ad6eb31eSJames Morse return err; 147ad6eb31eSJames Morse } 1482f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(invoke_sdei_fn); 149ad6eb31eSJames Morse 150ad6eb31eSJames Morse static struct sdei_event *sdei_event_find(u32 event_num) 151ad6eb31eSJames Morse { 152ad6eb31eSJames Morse struct sdei_event *e, *found = NULL; 153ad6eb31eSJames Morse 154ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 155ad6eb31eSJames Morse 156ad6eb31eSJames Morse spin_lock(&sdei_list_lock); 157ad6eb31eSJames Morse list_for_each_entry(e, &sdei_list, list) { 158ad6eb31eSJames Morse if (e->event_num == event_num) { 159ad6eb31eSJames Morse found = e; 160ad6eb31eSJames Morse break; 161ad6eb31eSJames Morse } 162ad6eb31eSJames Morse } 163ad6eb31eSJames Morse spin_unlock(&sdei_list_lock); 164ad6eb31eSJames Morse 165ad6eb31eSJames Morse return found; 166ad6eb31eSJames Morse } 167ad6eb31eSJames Morse 168ad6eb31eSJames Morse int sdei_api_event_context(u32 query, u64 *result) 169ad6eb31eSJames Morse { 170ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, query, 0, 0, 0, 0, 171ad6eb31eSJames Morse result); 172ad6eb31eSJames Morse } 173ad6eb31eSJames Morse NOKPROBE_SYMBOL(sdei_api_event_context); 174ad6eb31eSJames Morse 175ad6eb31eSJames Morse static int sdei_api_event_get_info(u32 event, u32 info, u64 *result) 176ad6eb31eSJames Morse { 177ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_GET_INFO, event, info, 0, 178ad6eb31eSJames Morse 0, 0, result); 179ad6eb31eSJames Morse } 180ad6eb31eSJames Morse 181ad6eb31eSJames Morse static struct sdei_event *sdei_event_create(u32 event_num, 182ad6eb31eSJames Morse sdei_event_callback *cb, 183ad6eb31eSJames Morse void *cb_arg) 184ad6eb31eSJames Morse { 185ad6eb31eSJames Morse int err; 186ad6eb31eSJames Morse u64 result; 187ad6eb31eSJames Morse struct sdei_event *event; 188ad6eb31eSJames Morse struct sdei_registered_event *reg; 189ad6eb31eSJames Morse 190ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 191ad6eb31eSJames Morse 192ad6eb31eSJames Morse event = kzalloc(sizeof(*event), GFP_KERNEL); 19311988424SGavin Shan if (!event) { 19411988424SGavin Shan err = -ENOMEM; 19511988424SGavin Shan goto fail; 19611988424SGavin Shan } 197ad6eb31eSJames Morse 198ad6eb31eSJames Morse INIT_LIST_HEAD(&event->list); 199ad6eb31eSJames Morse event->event_num = event_num; 200ad6eb31eSJames Morse 201ad6eb31eSJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY, 202ad6eb31eSJames Morse &result); 20311988424SGavin Shan if (err) 20411988424SGavin Shan goto fail; 205ad6eb31eSJames Morse event->priority = result; 206ad6eb31eSJames Morse 207ad6eb31eSJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_TYPE, 208ad6eb31eSJames Morse &result); 20911988424SGavin Shan if (err) 21011988424SGavin Shan goto fail; 211ad6eb31eSJames Morse event->type = result; 212ad6eb31eSJames Morse 213ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) { 214ad6eb31eSJames Morse reg = kzalloc(sizeof(*reg), GFP_KERNEL); 215ad6eb31eSJames Morse if (!reg) { 21611988424SGavin Shan err = -ENOMEM; 21711988424SGavin Shan goto fail; 218ad6eb31eSJames Morse } 219ad6eb31eSJames Morse 220ad6eb31eSJames Morse reg->event_num = event_num; 221ad6eb31eSJames Morse reg->priority = event->priority; 222ad6eb31eSJames Morse 223ad6eb31eSJames Morse reg->callback = cb; 224ad6eb31eSJames Morse reg->callback_arg = cb_arg; 225ad6eb31eSJames Morse event->registered = reg; 226f92b5462SJames Morse } else { 227f92b5462SJames Morse int cpu; 228f92b5462SJames Morse struct sdei_registered_event __percpu *regs; 229f92b5462SJames Morse 230f92b5462SJames Morse regs = alloc_percpu(struct sdei_registered_event); 231f92b5462SJames Morse if (!regs) { 23211988424SGavin Shan err = -ENOMEM; 23311988424SGavin Shan goto fail; 234f92b5462SJames Morse } 235f92b5462SJames Morse 236f92b5462SJames Morse for_each_possible_cpu(cpu) { 237f92b5462SJames Morse reg = per_cpu_ptr(regs, cpu); 238f92b5462SJames Morse 239f92b5462SJames Morse reg->event_num = event->event_num; 240f92b5462SJames Morse reg->priority = event->priority; 241f92b5462SJames Morse reg->callback = cb; 242f92b5462SJames Morse reg->callback_arg = cb_arg; 243f92b5462SJames Morse } 244f92b5462SJames Morse 245f92b5462SJames Morse event->private_registered = regs; 246ad6eb31eSJames Morse } 247ad6eb31eSJames Morse 248ad6eb31eSJames Morse spin_lock(&sdei_list_lock); 249ad6eb31eSJames Morse list_add(&event->list, &sdei_list); 250ad6eb31eSJames Morse spin_unlock(&sdei_list_lock); 251ad6eb31eSJames Morse 252ad6eb31eSJames Morse return event; 25311988424SGavin Shan 25411988424SGavin Shan fail: 25511988424SGavin Shan kfree(event); 25611988424SGavin Shan return ERR_PTR(err); 257ad6eb31eSJames Morse } 258ad6eb31eSJames Morse 259c66d52b1SLiguang Zhang static void sdei_event_destroy_llocked(struct sdei_event *event) 260ad6eb31eSJames Morse { 261ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 262c66d52b1SLiguang Zhang lockdep_assert_held(&sdei_list_lock); 263ad6eb31eSJames Morse 264ad6eb31eSJames Morse list_del(&event->list); 265ad6eb31eSJames Morse 266ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 267ad6eb31eSJames Morse kfree(event->registered); 268f92b5462SJames Morse else 269f92b5462SJames Morse free_percpu(event->private_registered); 270ad6eb31eSJames Morse 271ad6eb31eSJames Morse kfree(event); 272ad6eb31eSJames Morse } 273ad6eb31eSJames Morse 274c66d52b1SLiguang Zhang static void sdei_event_destroy(struct sdei_event *event) 275c66d52b1SLiguang Zhang { 276c66d52b1SLiguang Zhang spin_lock(&sdei_list_lock); 277c66d52b1SLiguang Zhang sdei_event_destroy_llocked(event); 278c66d52b1SLiguang Zhang spin_unlock(&sdei_list_lock); 279c66d52b1SLiguang Zhang } 280c66d52b1SLiguang Zhang 281ad6eb31eSJames Morse static int sdei_api_get_version(u64 *version) 282ad6eb31eSJames Morse { 283ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_VERSION, 0, 0, 0, 0, 0, version); 284ad6eb31eSJames Morse } 285ad6eb31eSJames Morse 286ad6eb31eSJames Morse int sdei_mask_local_cpu(void) 287ad6eb31eSJames Morse { 288ad6eb31eSJames Morse int err; 289ad6eb31eSJames Morse 290ad6eb31eSJames Morse WARN_ON_ONCE(preemptible()); 291ad6eb31eSJames Morse 292ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_MASK, 0, 0, 0, 0, 0, NULL); 293ad6eb31eSJames Morse if (err && err != -EIO) { 294ad6eb31eSJames Morse pr_warn_once("failed to mask CPU[%u]: %d\n", 295ad6eb31eSJames Morse smp_processor_id(), err); 296ad6eb31eSJames Morse return err; 297ad6eb31eSJames Morse } 298ad6eb31eSJames Morse 299ad6eb31eSJames Morse return 0; 300ad6eb31eSJames Morse } 301ad6eb31eSJames Morse 302ad6eb31eSJames Morse static void _ipi_mask_cpu(void *ignored) 303ad6eb31eSJames Morse { 304ad6eb31eSJames Morse sdei_mask_local_cpu(); 305ad6eb31eSJames Morse } 306ad6eb31eSJames Morse 307ad6eb31eSJames Morse int sdei_unmask_local_cpu(void) 308ad6eb31eSJames Morse { 309ad6eb31eSJames Morse int err; 310ad6eb31eSJames Morse 311ad6eb31eSJames Morse WARN_ON_ONCE(preemptible()); 312ad6eb31eSJames Morse 313ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_UNMASK, 0, 0, 0, 0, 0, NULL); 314ad6eb31eSJames Morse if (err && err != -EIO) { 315ad6eb31eSJames Morse pr_warn_once("failed to unmask CPU[%u]: %d\n", 316ad6eb31eSJames Morse smp_processor_id(), err); 317ad6eb31eSJames Morse return err; 318ad6eb31eSJames Morse } 319ad6eb31eSJames Morse 320ad6eb31eSJames Morse return 0; 321ad6eb31eSJames Morse } 322ad6eb31eSJames Morse 323ad6eb31eSJames Morse static void _ipi_unmask_cpu(void *ignored) 324ad6eb31eSJames Morse { 325ad6eb31eSJames Morse sdei_unmask_local_cpu(); 326ad6eb31eSJames Morse } 327ad6eb31eSJames Morse 328ad6eb31eSJames Morse static void _ipi_private_reset(void *ignored) 329ad6eb31eSJames Morse { 330ad6eb31eSJames Morse int err; 331ad6eb31eSJames Morse 332ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PRIVATE_RESET, 0, 0, 0, 0, 0, 333ad6eb31eSJames Morse NULL); 334ad6eb31eSJames Morse if (err && err != -EIO) 335ad6eb31eSJames Morse pr_warn_once("failed to reset CPU[%u]: %d\n", 336ad6eb31eSJames Morse smp_processor_id(), err); 337ad6eb31eSJames Morse } 338ad6eb31eSJames Morse 339ad6eb31eSJames Morse static int sdei_api_shared_reset(void) 340ad6eb31eSJames Morse { 341ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_SHARED_RESET, 0, 0, 0, 0, 0, 342ad6eb31eSJames Morse NULL); 343ad6eb31eSJames Morse } 344ad6eb31eSJames Morse 345ad6eb31eSJames Morse static void sdei_mark_interface_broken(void) 346ad6eb31eSJames Morse { 347ad6eb31eSJames Morse pr_err("disabling SDEI firmware interface\n"); 348ad6eb31eSJames Morse on_each_cpu(&_ipi_mask_cpu, NULL, true); 349ad6eb31eSJames Morse sdei_firmware_call = NULL; 350ad6eb31eSJames Morse } 351ad6eb31eSJames Morse 352ad6eb31eSJames Morse static int sdei_platform_reset(void) 353ad6eb31eSJames Morse { 354ad6eb31eSJames Morse int err; 355ad6eb31eSJames Morse 356ad6eb31eSJames Morse on_each_cpu(&_ipi_private_reset, NULL, true); 357ad6eb31eSJames Morse err = sdei_api_shared_reset(); 358ad6eb31eSJames Morse if (err) { 359ad6eb31eSJames Morse pr_err("Failed to reset platform: %d\n", err); 360ad6eb31eSJames Morse sdei_mark_interface_broken(); 361ad6eb31eSJames Morse } 362ad6eb31eSJames Morse 363ad6eb31eSJames Morse return err; 364ad6eb31eSJames Morse } 365ad6eb31eSJames Morse 366ad6eb31eSJames Morse static int sdei_api_event_enable(u32 event_num) 367ad6eb31eSJames Morse { 368ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_ENABLE, event_num, 0, 0, 0, 369ad6eb31eSJames Morse 0, NULL); 370ad6eb31eSJames Morse } 371ad6eb31eSJames Morse 372f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 373f92b5462SJames Morse static void _local_event_enable(void *data) 374f92b5462SJames Morse { 375f92b5462SJames Morse int err; 376f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 377f92b5462SJames Morse 378f92b5462SJames Morse WARN_ON_ONCE(preemptible()); 379f92b5462SJames Morse 380f92b5462SJames Morse err = sdei_api_event_enable(arg->event->event_num); 381f92b5462SJames Morse 382f92b5462SJames Morse sdei_cross_call_return(arg, err); 383f92b5462SJames Morse } 384f92b5462SJames Morse 385ad6eb31eSJames Morse int sdei_event_enable(u32 event_num) 386ad6eb31eSJames Morse { 387ad6eb31eSJames Morse int err = -EINVAL; 388ad6eb31eSJames Morse struct sdei_event *event; 389ad6eb31eSJames Morse 390ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 391ad6eb31eSJames Morse event = sdei_event_find(event_num); 392ad6eb31eSJames Morse if (!event) { 393ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 394ad6eb31eSJames Morse return -ENOENT; 395ad6eb31eSJames Morse } 396ad6eb31eSJames Morse 397da351827SJames Morse 39854f529a6SJames Morse cpus_read_lock(); 399ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 400ad6eb31eSJames Morse err = sdei_api_event_enable(event->event_num); 401f92b5462SJames Morse else 402f92b5462SJames Morse err = sdei_do_cross_call(_local_event_enable, event); 40354f529a6SJames Morse 40454f529a6SJames Morse if (!err) { 40554f529a6SJames Morse spin_lock(&sdei_list_lock); 40654f529a6SJames Morse event->reenable = true; 40754f529a6SJames Morse spin_unlock(&sdei_list_lock); 40854f529a6SJames Morse } 40954f529a6SJames Morse cpus_read_unlock(); 410ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 411ad6eb31eSJames Morse 412ad6eb31eSJames Morse return err; 413ad6eb31eSJames Morse } 414ad6eb31eSJames Morse 415ad6eb31eSJames Morse static int sdei_api_event_disable(u32 event_num) 416ad6eb31eSJames Morse { 417ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_DISABLE, event_num, 0, 0, 418ad6eb31eSJames Morse 0, 0, NULL); 419ad6eb31eSJames Morse } 420ad6eb31eSJames Morse 421f92b5462SJames Morse static void _ipi_event_disable(void *data) 422f92b5462SJames Morse { 423f92b5462SJames Morse int err; 424f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 425f92b5462SJames Morse 426f92b5462SJames Morse err = sdei_api_event_disable(arg->event->event_num); 427f92b5462SJames Morse 428f92b5462SJames Morse sdei_cross_call_return(arg, err); 429f92b5462SJames Morse } 430f92b5462SJames Morse 431ad6eb31eSJames Morse int sdei_event_disable(u32 event_num) 432ad6eb31eSJames Morse { 433ad6eb31eSJames Morse int err = -EINVAL; 434ad6eb31eSJames Morse struct sdei_event *event; 435ad6eb31eSJames Morse 436ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 437ad6eb31eSJames Morse event = sdei_event_find(event_num); 438ad6eb31eSJames Morse if (!event) { 439ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 440ad6eb31eSJames Morse return -ENOENT; 441ad6eb31eSJames Morse } 442ad6eb31eSJames Morse 443da351827SJames Morse spin_lock(&sdei_list_lock); 444da351827SJames Morse event->reenable = false; 445da351827SJames Morse spin_unlock(&sdei_list_lock); 446da351827SJames Morse 447ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 448ad6eb31eSJames Morse err = sdei_api_event_disable(event->event_num); 449f92b5462SJames Morse else 450f92b5462SJames Morse err = sdei_do_cross_call(_ipi_event_disable, event); 451ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 452ad6eb31eSJames Morse 453ad6eb31eSJames Morse return err; 454ad6eb31eSJames Morse } 455ad6eb31eSJames Morse 456ad6eb31eSJames Morse static int sdei_api_event_unregister(u32 event_num) 457ad6eb31eSJames Morse { 458ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_UNREGISTER, event_num, 0, 459ad6eb31eSJames Morse 0, 0, 0, NULL); 460ad6eb31eSJames Morse } 461ad6eb31eSJames Morse 462f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 463f92b5462SJames Morse static void _local_event_unregister(void *data) 464f92b5462SJames Morse { 465f92b5462SJames Morse int err; 466f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 467f92b5462SJames Morse 468f92b5462SJames Morse WARN_ON_ONCE(preemptible()); 469f92b5462SJames Morse 470f92b5462SJames Morse err = sdei_api_event_unregister(arg->event->event_num); 471f92b5462SJames Morse 472f92b5462SJames Morse sdei_cross_call_return(arg, err); 473f92b5462SJames Morse } 474f92b5462SJames Morse 475ad6eb31eSJames Morse static int _sdei_event_unregister(struct sdei_event *event) 476ad6eb31eSJames Morse { 477ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 478ad6eb31eSJames Morse 479ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 480ad6eb31eSJames Morse return sdei_api_event_unregister(event->event_num); 481ad6eb31eSJames Morse 482f92b5462SJames Morse return sdei_do_cross_call(_local_event_unregister, event); 483ad6eb31eSJames Morse } 484ad6eb31eSJames Morse 485ad6eb31eSJames Morse int sdei_event_unregister(u32 event_num) 486ad6eb31eSJames Morse { 487ad6eb31eSJames Morse int err; 488ad6eb31eSJames Morse struct sdei_event *event; 489ad6eb31eSJames Morse 490ad6eb31eSJames Morse WARN_ON(in_nmi()); 491ad6eb31eSJames Morse 492ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 493ad6eb31eSJames Morse event = sdei_event_find(event_num); 494ad6eb31eSJames Morse do { 495ad6eb31eSJames Morse if (!event) { 496ad6eb31eSJames Morse pr_warn("Event %u not registered\n", event_num); 497ad6eb31eSJames Morse err = -ENOENT; 498ad6eb31eSJames Morse break; 499ad6eb31eSJames Morse } 500ad6eb31eSJames Morse 5016ded0b61SJames Morse spin_lock(&sdei_list_lock); 5026ded0b61SJames Morse event->reregister = false; 5036ded0b61SJames Morse event->reenable = false; 5046ded0b61SJames Morse spin_unlock(&sdei_list_lock); 5056ded0b61SJames Morse 506ad6eb31eSJames Morse err = _sdei_event_unregister(event); 507ad6eb31eSJames Morse if (err) 508ad6eb31eSJames Morse break; 509ad6eb31eSJames Morse 510ad6eb31eSJames Morse sdei_event_destroy(event); 511ad6eb31eSJames Morse } while (0); 512ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 513ad6eb31eSJames Morse 514ad6eb31eSJames Morse return err; 515ad6eb31eSJames Morse } 516ad6eb31eSJames Morse 517da351827SJames Morse /* 518da351827SJames Morse * unregister events, but don't destroy them as they are re-registered by 519da351827SJames Morse * sdei_reregister_shared(). 520da351827SJames Morse */ 521da351827SJames Morse static int sdei_unregister_shared(void) 522da351827SJames Morse { 523da351827SJames Morse int err = 0; 524da351827SJames Morse struct sdei_event *event; 525da351827SJames Morse 526da351827SJames Morse mutex_lock(&sdei_events_lock); 527da351827SJames Morse spin_lock(&sdei_list_lock); 528da351827SJames Morse list_for_each_entry(event, &sdei_list, list) { 529da351827SJames Morse if (event->type != SDEI_EVENT_TYPE_SHARED) 530da351827SJames Morse continue; 531da351827SJames Morse 532da351827SJames Morse err = _sdei_event_unregister(event); 533da351827SJames Morse if (err) 534da351827SJames Morse break; 535da351827SJames Morse } 536da351827SJames Morse spin_unlock(&sdei_list_lock); 537da351827SJames Morse mutex_unlock(&sdei_events_lock); 538da351827SJames Morse 539da351827SJames Morse return err; 540da351827SJames Morse } 541da351827SJames Morse 542ad6eb31eSJames Morse static int sdei_api_event_register(u32 event_num, unsigned long entry_point, 543ad6eb31eSJames Morse void *arg, u64 flags, u64 affinity) 544ad6eb31eSJames Morse { 545ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_REGISTER, event_num, 546ad6eb31eSJames Morse (unsigned long)entry_point, (unsigned long)arg, 547ad6eb31eSJames Morse flags, affinity, NULL); 548ad6eb31eSJames Morse } 549ad6eb31eSJames Morse 550f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 551f92b5462SJames Morse static void _local_event_register(void *data) 552f92b5462SJames Morse { 553f92b5462SJames Morse int err; 554f92b5462SJames Morse struct sdei_registered_event *reg; 555f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 556f92b5462SJames Morse 557f92b5462SJames Morse WARN_ON(preemptible()); 558f92b5462SJames Morse 559f92b5462SJames Morse reg = per_cpu_ptr(arg->event->private_registered, smp_processor_id()); 560f92b5462SJames Morse err = sdei_api_event_register(arg->event->event_num, sdei_entry_point, 561f92b5462SJames Morse reg, 0, 0); 562f92b5462SJames Morse 563f92b5462SJames Morse sdei_cross_call_return(arg, err); 564f92b5462SJames Morse } 565f92b5462SJames Morse 566ad6eb31eSJames Morse static int _sdei_event_register(struct sdei_event *event) 567ad6eb31eSJames Morse { 568f92b5462SJames Morse int err; 569f92b5462SJames Morse 570ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 571ad6eb31eSJames Morse 572ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 573ad6eb31eSJames Morse return sdei_api_event_register(event->event_num, 574ad6eb31eSJames Morse sdei_entry_point, 575ad6eb31eSJames Morse event->registered, 576ad6eb31eSJames Morse SDEI_EVENT_REGISTER_RM_ANY, 0); 577ad6eb31eSJames Morse 578f92b5462SJames Morse err = sdei_do_cross_call(_local_event_register, event); 5796ded0b61SJames Morse if (err) 580f92b5462SJames Morse sdei_do_cross_call(_local_event_unregister, event); 581f92b5462SJames Morse 582f92b5462SJames Morse return err; 583ad6eb31eSJames Morse } 584ad6eb31eSJames Morse 585ad6eb31eSJames Morse int sdei_event_register(u32 event_num, sdei_event_callback *cb, void *arg) 586ad6eb31eSJames Morse { 587ad6eb31eSJames Morse int err; 588ad6eb31eSJames Morse struct sdei_event *event; 589ad6eb31eSJames Morse 590ad6eb31eSJames Morse WARN_ON(in_nmi()); 591ad6eb31eSJames Morse 592ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 593ad6eb31eSJames Morse do { 594ad6eb31eSJames Morse if (sdei_event_find(event_num)) { 595ad6eb31eSJames Morse pr_warn("Event %u already registered\n", event_num); 596ad6eb31eSJames Morse err = -EBUSY; 597ad6eb31eSJames Morse break; 598ad6eb31eSJames Morse } 599ad6eb31eSJames Morse 600ad6eb31eSJames Morse event = sdei_event_create(event_num, cb, arg); 601ad6eb31eSJames Morse if (IS_ERR(event)) { 602ad6eb31eSJames Morse err = PTR_ERR(event); 603ad6eb31eSJames Morse pr_warn("Failed to create event %u: %d\n", event_num, 604ad6eb31eSJames Morse err); 605ad6eb31eSJames Morse break; 606ad6eb31eSJames Morse } 607ad6eb31eSJames Morse 60854f529a6SJames Morse cpus_read_lock(); 609ad6eb31eSJames Morse err = _sdei_event_register(event); 610ad6eb31eSJames Morse if (err) { 611ad6eb31eSJames Morse sdei_event_destroy(event); 612ad6eb31eSJames Morse pr_warn("Failed to register event %u: %d\n", event_num, 613ad6eb31eSJames Morse err); 61454f529a6SJames Morse } else { 61554f529a6SJames Morse spin_lock(&sdei_list_lock); 61654f529a6SJames Morse event->reregister = true; 61754f529a6SJames Morse spin_unlock(&sdei_list_lock); 618ad6eb31eSJames Morse } 61954f529a6SJames Morse cpus_read_unlock(); 620ad6eb31eSJames Morse } while (0); 621ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 622ad6eb31eSJames Morse 623ad6eb31eSJames Morse return err; 624ad6eb31eSJames Morse } 625ad6eb31eSJames Morse 626c66d52b1SLiguang Zhang static int sdei_reregister_event_llocked(struct sdei_event *event) 627da351827SJames Morse { 628da351827SJames Morse int err; 629da351827SJames Morse 630da351827SJames Morse lockdep_assert_held(&sdei_events_lock); 631c66d52b1SLiguang Zhang lockdep_assert_held(&sdei_list_lock); 632da351827SJames Morse 633da351827SJames Morse err = _sdei_event_register(event); 634da351827SJames Morse if (err) { 635da351827SJames Morse pr_err("Failed to re-register event %u\n", event->event_num); 636c66d52b1SLiguang Zhang sdei_event_destroy_llocked(event); 637da351827SJames Morse return err; 638da351827SJames Morse } 639da351827SJames Morse 640da351827SJames Morse if (event->reenable) { 641da351827SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 642da351827SJames Morse err = sdei_api_event_enable(event->event_num); 643f92b5462SJames Morse else 644f92b5462SJames Morse err = sdei_do_cross_call(_local_event_enable, event); 645da351827SJames Morse } 646da351827SJames Morse 647da351827SJames Morse if (err) 648da351827SJames Morse pr_err("Failed to re-enable event %u\n", event->event_num); 649da351827SJames Morse 650da351827SJames Morse return err; 651da351827SJames Morse } 652da351827SJames Morse 653da351827SJames Morse static int sdei_reregister_shared(void) 654da351827SJames Morse { 655da351827SJames Morse int err = 0; 656da351827SJames Morse struct sdei_event *event; 657da351827SJames Morse 658da351827SJames Morse mutex_lock(&sdei_events_lock); 659da351827SJames Morse spin_lock(&sdei_list_lock); 660da351827SJames Morse list_for_each_entry(event, &sdei_list, list) { 661da351827SJames Morse if (event->type != SDEI_EVENT_TYPE_SHARED) 662da351827SJames Morse continue; 663da351827SJames Morse 664da351827SJames Morse if (event->reregister) { 665c66d52b1SLiguang Zhang err = sdei_reregister_event_llocked(event); 666da351827SJames Morse if (err) 667da351827SJames Morse break; 668da351827SJames Morse } 669da351827SJames Morse } 670da351827SJames Morse spin_unlock(&sdei_list_lock); 671da351827SJames Morse mutex_unlock(&sdei_events_lock); 672da351827SJames Morse 673da351827SJames Morse return err; 674da351827SJames Morse } 675da351827SJames Morse 676f92b5462SJames Morse static int sdei_cpuhp_down(unsigned int cpu) 677f92b5462SJames Morse { 678f92b5462SJames Morse struct sdei_event *event; 679f92b5462SJames Morse struct sdei_crosscall_args arg; 680f92b5462SJames Morse 681f92b5462SJames Morse /* un-register private events */ 682f92b5462SJames Morse spin_lock(&sdei_list_lock); 683f92b5462SJames Morse list_for_each_entry(event, &sdei_list, list) { 684f92b5462SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 685f92b5462SJames Morse continue; 686f92b5462SJames Morse 687f92b5462SJames Morse CROSSCALL_INIT(arg, event); 688f92b5462SJames Morse /* call the cross-call function locally... */ 689f92b5462SJames Morse _local_event_unregister(&arg); 690f92b5462SJames Morse if (arg.first_error) 691f92b5462SJames Morse pr_err("Failed to unregister event %u: %d\n", 692f92b5462SJames Morse event->event_num, arg.first_error); 693f92b5462SJames Morse } 694f92b5462SJames Morse spin_unlock(&sdei_list_lock); 695f92b5462SJames Morse 696f92b5462SJames Morse return sdei_mask_local_cpu(); 697f92b5462SJames Morse } 698f92b5462SJames Morse 699f92b5462SJames Morse static int sdei_cpuhp_up(unsigned int cpu) 700f92b5462SJames Morse { 701f92b5462SJames Morse struct sdei_event *event; 702f92b5462SJames Morse struct sdei_crosscall_args arg; 703f92b5462SJames Morse 704f92b5462SJames Morse /* re-register/enable private events */ 705f92b5462SJames Morse spin_lock(&sdei_list_lock); 706f92b5462SJames Morse list_for_each_entry(event, &sdei_list, list) { 707f92b5462SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 708f92b5462SJames Morse continue; 709f92b5462SJames Morse 710f92b5462SJames Morse if (event->reregister) { 711f92b5462SJames Morse CROSSCALL_INIT(arg, event); 712f92b5462SJames Morse /* call the cross-call function locally... */ 713f92b5462SJames Morse _local_event_register(&arg); 714f92b5462SJames Morse if (arg.first_error) 715f92b5462SJames Morse pr_err("Failed to re-register event %u: %d\n", 716f92b5462SJames Morse event->event_num, arg.first_error); 717f92b5462SJames Morse } 718f92b5462SJames Morse 719f92b5462SJames Morse if (event->reenable) { 720f92b5462SJames Morse CROSSCALL_INIT(arg, event); 721f92b5462SJames Morse _local_event_enable(&arg); 722f92b5462SJames Morse if (arg.first_error) 723f92b5462SJames Morse pr_err("Failed to re-enable event %u: %d\n", 724f92b5462SJames Morse event->event_num, arg.first_error); 725f92b5462SJames Morse } 726f92b5462SJames Morse } 727f92b5462SJames Morse spin_unlock(&sdei_list_lock); 728f92b5462SJames Morse 729f92b5462SJames Morse return sdei_unmask_local_cpu(); 730f92b5462SJames Morse } 731f92b5462SJames Morse 732da351827SJames Morse /* When entering idle, mask/unmask events for this cpu */ 733da351827SJames Morse static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action, 734da351827SJames Morse void *data) 735da351827SJames Morse { 736da351827SJames Morse int rv; 737da351827SJames Morse 738da351827SJames Morse switch (action) { 739da351827SJames Morse case CPU_PM_ENTER: 740da351827SJames Morse rv = sdei_mask_local_cpu(); 741da351827SJames Morse break; 742da351827SJames Morse case CPU_PM_EXIT: 743da351827SJames Morse case CPU_PM_ENTER_FAILED: 744da351827SJames Morse rv = sdei_unmask_local_cpu(); 745da351827SJames Morse break; 746da351827SJames Morse default: 747da351827SJames Morse return NOTIFY_DONE; 748da351827SJames Morse } 749da351827SJames Morse 750da351827SJames Morse if (rv) 751da351827SJames Morse return notifier_from_errno(rv); 752da351827SJames Morse 753da351827SJames Morse return NOTIFY_OK; 754da351827SJames Morse } 755da351827SJames Morse 756da351827SJames Morse static struct notifier_block sdei_pm_nb = { 757da351827SJames Morse .notifier_call = sdei_pm_notifier, 758da351827SJames Morse }; 759da351827SJames Morse 760da351827SJames Morse static int sdei_device_suspend(struct device *dev) 761da351827SJames Morse { 762da351827SJames Morse on_each_cpu(_ipi_mask_cpu, NULL, true); 763da351827SJames Morse 764da351827SJames Morse return 0; 765da351827SJames Morse } 766da351827SJames Morse 767da351827SJames Morse static int sdei_device_resume(struct device *dev) 768da351827SJames Morse { 769da351827SJames Morse on_each_cpu(_ipi_unmask_cpu, NULL, true); 770da351827SJames Morse 771da351827SJames Morse return 0; 772da351827SJames Morse } 773da351827SJames Morse 774da351827SJames Morse /* 775da351827SJames Morse * We need all events to be reregistered when we resume from hibernate. 776da351827SJames Morse * 777da351827SJames Morse * The sequence is freeze->thaw. Reboot. freeze->restore. We unregister 778da351827SJames Morse * events during freeze, then re-register and re-enable them during thaw 779da351827SJames Morse * and restore. 780da351827SJames Morse */ 781da351827SJames Morse static int sdei_device_freeze(struct device *dev) 782da351827SJames Morse { 783da351827SJames Morse int err; 784da351827SJames Morse 785f92b5462SJames Morse /* unregister private events */ 786da351827SJames Morse cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); 787da351827SJames Morse 788da351827SJames Morse err = sdei_unregister_shared(); 789da351827SJames Morse if (err) 790da351827SJames Morse return err; 791da351827SJames Morse 792da351827SJames Morse return 0; 793da351827SJames Morse } 794da351827SJames Morse 795da351827SJames Morse static int sdei_device_thaw(struct device *dev) 796da351827SJames Morse { 797da351827SJames Morse int err; 798da351827SJames Morse 799da351827SJames Morse /* re-register shared events */ 800da351827SJames Morse err = sdei_reregister_shared(); 801da351827SJames Morse if (err) { 802da351827SJames Morse pr_warn("Failed to re-register shared events...\n"); 803da351827SJames Morse sdei_mark_interface_broken(); 804da351827SJames Morse return err; 805da351827SJames Morse } 806da351827SJames Morse 807da351827SJames Morse err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", 808da351827SJames Morse &sdei_cpuhp_up, &sdei_cpuhp_down); 809da351827SJames Morse if (err) 810da351827SJames Morse pr_warn("Failed to re-register CPU hotplug notifier...\n"); 811da351827SJames Morse 812da351827SJames Morse return err; 813da351827SJames Morse } 814da351827SJames Morse 815da351827SJames Morse static int sdei_device_restore(struct device *dev) 816da351827SJames Morse { 817da351827SJames Morse int err; 818da351827SJames Morse 819da351827SJames Morse err = sdei_platform_reset(); 820da351827SJames Morse if (err) 821da351827SJames Morse return err; 822da351827SJames Morse 823da351827SJames Morse return sdei_device_thaw(dev); 824da351827SJames Morse } 825da351827SJames Morse 826da351827SJames Morse static const struct dev_pm_ops sdei_pm_ops = { 827da351827SJames Morse .suspend = sdei_device_suspend, 828da351827SJames Morse .resume = sdei_device_resume, 829da351827SJames Morse .freeze = sdei_device_freeze, 830da351827SJames Morse .thaw = sdei_device_thaw, 831da351827SJames Morse .restore = sdei_device_restore, 832da351827SJames Morse }; 833da351827SJames Morse 834da351827SJames Morse /* 835da351827SJames Morse * Mask all CPUs and unregister all events on panic, reboot or kexec. 836da351827SJames Morse */ 837da351827SJames Morse static int sdei_reboot_notifier(struct notifier_block *nb, unsigned long action, 838da351827SJames Morse void *data) 839da351827SJames Morse { 840da351827SJames Morse /* 841da351827SJames Morse * We are going to reset the interface, after this there is no point 842da351827SJames Morse * doing work when we take CPUs offline. 843da351827SJames Morse */ 844da351827SJames Morse cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); 845da351827SJames Morse 846da351827SJames Morse sdei_platform_reset(); 847da351827SJames Morse 848da351827SJames Morse return NOTIFY_OK; 849da351827SJames Morse } 850da351827SJames Morse 851da351827SJames Morse static struct notifier_block sdei_reboot_nb = { 852da351827SJames Morse .notifier_call = sdei_reboot_notifier, 853da351827SJames Morse }; 854da351827SJames Morse 855ad6eb31eSJames Morse static void sdei_smccc_smc(unsigned long function_id, 856ad6eb31eSJames Morse unsigned long arg0, unsigned long arg1, 857ad6eb31eSJames Morse unsigned long arg2, unsigned long arg3, 858ad6eb31eSJames Morse unsigned long arg4, struct arm_smccc_res *res) 859ad6eb31eSJames Morse { 860ad6eb31eSJames Morse arm_smccc_smc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); 861ad6eb31eSJames Morse } 8622f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(sdei_smccc_smc); 863ad6eb31eSJames Morse 864ad6eb31eSJames Morse static void sdei_smccc_hvc(unsigned long function_id, 865ad6eb31eSJames Morse unsigned long arg0, unsigned long arg1, 866ad6eb31eSJames Morse unsigned long arg2, unsigned long arg3, 867ad6eb31eSJames Morse unsigned long arg4, struct arm_smccc_res *res) 868ad6eb31eSJames Morse { 869ad6eb31eSJames Morse arm_smccc_hvc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); 870ad6eb31eSJames Morse } 8712f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(sdei_smccc_hvc); 872ad6eb31eSJames Morse 873f96935d3SJames Morse int sdei_register_ghes(struct ghes *ghes, sdei_event_callback *normal_cb, 874f96935d3SJames Morse sdei_event_callback *critical_cb) 875f96935d3SJames Morse { 876f96935d3SJames Morse int err; 877f96935d3SJames Morse u64 result; 878f96935d3SJames Morse u32 event_num; 879f96935d3SJames Morse sdei_event_callback *cb; 880f96935d3SJames Morse 881f96935d3SJames Morse if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES)) 882f96935d3SJames Morse return -EOPNOTSUPP; 883f96935d3SJames Morse 884f96935d3SJames Morse event_num = ghes->generic->notify.vector; 885f96935d3SJames Morse if (event_num == 0) { 886f96935d3SJames Morse /* 887f96935d3SJames Morse * Event 0 is reserved by the specification for 888f96935d3SJames Morse * SDEI_EVENT_SIGNAL. 889f96935d3SJames Morse */ 890f96935d3SJames Morse return -EINVAL; 891f96935d3SJames Morse } 892f96935d3SJames Morse 893f96935d3SJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY, 894f96935d3SJames Morse &result); 895f96935d3SJames Morse if (err) 896f96935d3SJames Morse return err; 897f96935d3SJames Morse 898f96935d3SJames Morse if (result == SDEI_EVENT_PRIORITY_CRITICAL) 899f96935d3SJames Morse cb = critical_cb; 900f96935d3SJames Morse else 901f96935d3SJames Morse cb = normal_cb; 902f96935d3SJames Morse 903f96935d3SJames Morse err = sdei_event_register(event_num, cb, ghes); 904f96935d3SJames Morse if (!err) 905f96935d3SJames Morse err = sdei_event_enable(event_num); 906f96935d3SJames Morse 907f96935d3SJames Morse return err; 908f96935d3SJames Morse } 909f96935d3SJames Morse 910f96935d3SJames Morse int sdei_unregister_ghes(struct ghes *ghes) 911f96935d3SJames Morse { 912f96935d3SJames Morse int i; 913f96935d3SJames Morse int err; 914f96935d3SJames Morse u32 event_num = ghes->generic->notify.vector; 915f96935d3SJames Morse 916f96935d3SJames Morse might_sleep(); 917f96935d3SJames Morse 918f96935d3SJames Morse if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES)) 919f96935d3SJames Morse return -EOPNOTSUPP; 920f96935d3SJames Morse 921f96935d3SJames Morse /* 922f96935d3SJames Morse * The event may be running on another CPU. Disable it 923f96935d3SJames Morse * to stop new events, then try to unregister a few times. 924f96935d3SJames Morse */ 925f96935d3SJames Morse err = sdei_event_disable(event_num); 926f96935d3SJames Morse if (err) 927f96935d3SJames Morse return err; 928f96935d3SJames Morse 929f96935d3SJames Morse for (i = 0; i < 3; i++) { 930f96935d3SJames Morse err = sdei_event_unregister(event_num); 931f96935d3SJames Morse if (err != -EINPROGRESS) 932f96935d3SJames Morse break; 933f96935d3SJames Morse 934f96935d3SJames Morse schedule(); 935f96935d3SJames Morse } 936f96935d3SJames Morse 937f96935d3SJames Morse return err; 938f96935d3SJames Morse } 939f96935d3SJames Morse 940ad6eb31eSJames Morse static int sdei_get_conduit(struct platform_device *pdev) 941ad6eb31eSJames Morse { 942ad6eb31eSJames Morse const char *method; 943ad6eb31eSJames Morse struct device_node *np = pdev->dev.of_node; 944ad6eb31eSJames Morse 945ad6eb31eSJames Morse sdei_firmware_call = NULL; 946ad6eb31eSJames Morse if (np) { 947ad6eb31eSJames Morse if (of_property_read_string(np, "method", &method)) { 948ad6eb31eSJames Morse pr_warn("missing \"method\" property\n"); 949e6ea4651SMark Rutland return SMCCC_CONDUIT_NONE; 950ad6eb31eSJames Morse } 951ad6eb31eSJames Morse 952ad6eb31eSJames Morse if (!strcmp("hvc", method)) { 953ad6eb31eSJames Morse sdei_firmware_call = &sdei_smccc_hvc; 954e6ea4651SMark Rutland return SMCCC_CONDUIT_HVC; 955ad6eb31eSJames Morse } else if (!strcmp("smc", method)) { 956ad6eb31eSJames Morse sdei_firmware_call = &sdei_smccc_smc; 957e6ea4651SMark Rutland return SMCCC_CONDUIT_SMC; 958ad6eb31eSJames Morse } 959ad6eb31eSJames Morse 960ad6eb31eSJames Morse pr_warn("invalid \"method\" property: %s\n", method); 961677a60bdSJames Morse } else if (IS_ENABLED(CONFIG_ACPI) && !acpi_disabled) { 962677a60bdSJames Morse if (acpi_psci_use_hvc()) { 963677a60bdSJames Morse sdei_firmware_call = &sdei_smccc_hvc; 964e6ea4651SMark Rutland return SMCCC_CONDUIT_HVC; 965677a60bdSJames Morse } else { 966677a60bdSJames Morse sdei_firmware_call = &sdei_smccc_smc; 967e6ea4651SMark Rutland return SMCCC_CONDUIT_SMC; 968677a60bdSJames Morse } 969ad6eb31eSJames Morse } 970ad6eb31eSJames Morse 971e6ea4651SMark Rutland return SMCCC_CONDUIT_NONE; 972ad6eb31eSJames Morse } 973ad6eb31eSJames Morse 974ad6eb31eSJames Morse static int sdei_probe(struct platform_device *pdev) 975ad6eb31eSJames Morse { 976ad6eb31eSJames Morse int err; 977ad6eb31eSJames Morse u64 ver = 0; 978ad6eb31eSJames Morse int conduit; 979ad6eb31eSJames Morse 980ad6eb31eSJames Morse conduit = sdei_get_conduit(pdev); 981ad6eb31eSJames Morse if (!sdei_firmware_call) 982ad6eb31eSJames Morse return 0; 983ad6eb31eSJames Morse 984ad6eb31eSJames Morse err = sdei_api_get_version(&ver); 985ad6eb31eSJames Morse if (err == -EOPNOTSUPP) 986ad6eb31eSJames Morse pr_err("advertised but not implemented in platform firmware\n"); 987ad6eb31eSJames Morse if (err) { 988ad6eb31eSJames Morse pr_err("Failed to get SDEI version: %d\n", err); 989ad6eb31eSJames Morse sdei_mark_interface_broken(); 990ad6eb31eSJames Morse return err; 991ad6eb31eSJames Morse } 992ad6eb31eSJames Morse 993ad6eb31eSJames Morse pr_info("SDEIv%d.%d (0x%x) detected in firmware.\n", 994ad6eb31eSJames Morse (int)SDEI_VERSION_MAJOR(ver), (int)SDEI_VERSION_MINOR(ver), 995ad6eb31eSJames Morse (int)SDEI_VERSION_VENDOR(ver)); 996ad6eb31eSJames Morse 997ad6eb31eSJames Morse if (SDEI_VERSION_MAJOR(ver) != 1) { 998ad6eb31eSJames Morse pr_warn("Conflicting SDEI version detected.\n"); 999ad6eb31eSJames Morse sdei_mark_interface_broken(); 1000ad6eb31eSJames Morse return -EINVAL; 1001ad6eb31eSJames Morse } 1002ad6eb31eSJames Morse 1003ad6eb31eSJames Morse err = sdei_platform_reset(); 1004ad6eb31eSJames Morse if (err) 1005ad6eb31eSJames Morse return err; 1006ad6eb31eSJames Morse 1007ad6eb31eSJames Morse sdei_entry_point = sdei_arch_get_entry_point(conduit); 1008ad6eb31eSJames Morse if (!sdei_entry_point) { 1009ad6eb31eSJames Morse /* Not supported due to hardware or boot configuration */ 1010ad6eb31eSJames Morse sdei_mark_interface_broken(); 1011ad6eb31eSJames Morse return 0; 1012ad6eb31eSJames Morse } 1013ad6eb31eSJames Morse 1014da351827SJames Morse err = cpu_pm_register_notifier(&sdei_pm_nb); 1015da351827SJames Morse if (err) { 1016da351827SJames Morse pr_warn("Failed to register CPU PM notifier...\n"); 1017da351827SJames Morse goto error; 1018da351827SJames Morse } 1019da351827SJames Morse 1020da351827SJames Morse err = register_reboot_notifier(&sdei_reboot_nb); 1021da351827SJames Morse if (err) { 1022da351827SJames Morse pr_warn("Failed to register reboot notifier...\n"); 1023da351827SJames Morse goto remove_cpupm; 1024da351827SJames Morse } 1025da351827SJames Morse 1026da351827SJames Morse err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", 1027da351827SJames Morse &sdei_cpuhp_up, &sdei_cpuhp_down); 1028da351827SJames Morse if (err) { 1029da351827SJames Morse pr_warn("Failed to register CPU hotplug notifier...\n"); 1030da351827SJames Morse goto remove_reboot; 1031da351827SJames Morse } 1032ad6eb31eSJames Morse 1033ad6eb31eSJames Morse return 0; 1034da351827SJames Morse 1035da351827SJames Morse remove_reboot: 1036da351827SJames Morse unregister_reboot_notifier(&sdei_reboot_nb); 1037da351827SJames Morse 1038da351827SJames Morse remove_cpupm: 1039da351827SJames Morse cpu_pm_unregister_notifier(&sdei_pm_nb); 1040da351827SJames Morse 1041da351827SJames Morse error: 1042da351827SJames Morse sdei_mark_interface_broken(); 1043da351827SJames Morse return err; 1044ad6eb31eSJames Morse } 1045ad6eb31eSJames Morse 1046ad6eb31eSJames Morse static const struct of_device_id sdei_of_match[] = { 1047ad6eb31eSJames Morse { .compatible = "arm,sdei-1.0" }, 1048ad6eb31eSJames Morse {} 1049ad6eb31eSJames Morse }; 1050ad6eb31eSJames Morse 1051ad6eb31eSJames Morse static struct platform_driver sdei_driver = { 1052ad6eb31eSJames Morse .driver = { 1053ad6eb31eSJames Morse .name = "sdei", 1054da351827SJames Morse .pm = &sdei_pm_ops, 1055ad6eb31eSJames Morse .of_match_table = sdei_of_match, 1056ad6eb31eSJames Morse }, 1057ad6eb31eSJames Morse .probe = sdei_probe, 1058ad6eb31eSJames Morse }; 1059ad6eb31eSJames Morse 1060677a60bdSJames Morse static bool __init sdei_present_acpi(void) 1061677a60bdSJames Morse { 1062677a60bdSJames Morse acpi_status status; 1063677a60bdSJames Morse struct acpi_table_header *sdei_table_header; 1064677a60bdSJames Morse 1065677a60bdSJames Morse if (acpi_disabled) 1066677a60bdSJames Morse return false; 1067677a60bdSJames Morse 1068677a60bdSJames Morse status = acpi_get_table(ACPI_SIG_SDEI, 0, &sdei_table_header); 1069677a60bdSJames Morse if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1070677a60bdSJames Morse const char *msg = acpi_format_exception(status); 1071677a60bdSJames Morse 1072677a60bdSJames Morse pr_info("Failed to get ACPI:SDEI table, %s\n", msg); 1073677a60bdSJames Morse } 1074677a60bdSJames Morse if (ACPI_FAILURE(status)) 1075677a60bdSJames Morse return false; 1076677a60bdSJames Morse 107770e6352aSHanjun Guo acpi_put_table(sdei_table_header); 107870e6352aSHanjun Guo 1079677a60bdSJames Morse return true; 1080677a60bdSJames Morse } 1081677a60bdSJames Morse 1082ad6eb31eSJames Morse static int __init sdei_init(void) 1083ad6eb31eSJames Morse { 1084caf2cd61SSudeep Holla int ret = platform_driver_register(&sdei_driver); 1085ad6eb31eSJames Morse 1086caf2cd61SSudeep Holla if (!ret && sdei_present_acpi()) { 1087caf2cd61SSudeep Holla struct platform_device *pdev; 1088caf2cd61SSudeep Holla 1089caf2cd61SSudeep Holla pdev = platform_device_register_simple(sdei_driver.driver.name, 1090caf2cd61SSudeep Holla 0, NULL, 0); 1091caf2cd61SSudeep Holla if (IS_ERR(pdev)) 1092caf2cd61SSudeep Holla pr_info("Failed to register ACPI:SDEI platform device %ld\n", 1093caf2cd61SSudeep Holla PTR_ERR(pdev)); 1094caf2cd61SSudeep Holla } 1095caf2cd61SSudeep Holla 1096caf2cd61SSudeep Holla return ret; 1097ad6eb31eSJames Morse } 1098ad6eb31eSJames Morse 1099677a60bdSJames Morse /* 1100677a60bdSJames Morse * On an ACPI system SDEI needs to be ready before HEST:GHES tries to register 1101677a60bdSJames Morse * its events. ACPI is initialised from a subsys_initcall(), GHES is initialised 1102677a60bdSJames Morse * by device_initcall(). We want to be called in the middle. 1103677a60bdSJames Morse */ 1104ad6eb31eSJames Morse subsys_initcall_sync(sdei_init); 1105ad6eb31eSJames Morse 1106ad6eb31eSJames Morse int sdei_event_handler(struct pt_regs *regs, 1107ad6eb31eSJames Morse struct sdei_registered_event *arg) 1108ad6eb31eSJames Morse { 1109ad6eb31eSJames Morse int err; 1110ad6eb31eSJames Morse mm_segment_t orig_addr_limit; 1111ad6eb31eSJames Morse u32 event_num = arg->event_num; 1112ad6eb31eSJames Morse 1113472de63bSJames Morse /* 1114472de63bSJames Morse * Save restore 'fs'. 1115472de63bSJames Morse * The architecture's entry code save/restores 'fs' when taking an 1116472de63bSJames Morse * exception from the kernel. This ensures addr_limit isn't inherited 1117472de63bSJames Morse * if you interrupted something that allowed the uaccess routines to 1118472de63bSJames Morse * access kernel memory. 1119472de63bSJames Morse * Do the same here because this doesn't come via the same entry code. 1120472de63bSJames Morse */ 11213d13f313SChristoph Hellwig orig_addr_limit = force_uaccess_begin(); 1122ad6eb31eSJames Morse 1123ad6eb31eSJames Morse err = arg->callback(event_num, regs, arg->callback_arg); 1124ad6eb31eSJames Morse if (err) 1125ad6eb31eSJames Morse pr_err_ratelimited("event %u on CPU %u failed with error: %d\n", 1126ad6eb31eSJames Morse event_num, smp_processor_id(), err); 1127ad6eb31eSJames Morse 11283d13f313SChristoph Hellwig force_uaccess_end(orig_addr_limit); 1129ad6eb31eSJames Morse 1130ad6eb31eSJames Morse return err; 1131ad6eb31eSJames Morse } 1132ad6eb31eSJames Morse NOKPROBE_SYMBOL(sdei_event_handler); 1133