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 117*5735f515SGavin 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 { 125*5735f515SGavin 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); 193ad6eb31eSJames Morse if (!event) 194ad6eb31eSJames Morse return ERR_PTR(-ENOMEM); 195ad6eb31eSJames Morse 196ad6eb31eSJames Morse INIT_LIST_HEAD(&event->list); 197ad6eb31eSJames Morse event->event_num = event_num; 198ad6eb31eSJames Morse 199ad6eb31eSJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY, 200ad6eb31eSJames Morse &result); 201ad6eb31eSJames Morse if (err) { 202ad6eb31eSJames Morse kfree(event); 203ad6eb31eSJames Morse return ERR_PTR(err); 204ad6eb31eSJames Morse } 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); 209ad6eb31eSJames Morse if (err) { 210ad6eb31eSJames Morse kfree(event); 211ad6eb31eSJames Morse return ERR_PTR(err); 212ad6eb31eSJames Morse } 213ad6eb31eSJames Morse event->type = result; 214ad6eb31eSJames Morse 215ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) { 216ad6eb31eSJames Morse reg = kzalloc(sizeof(*reg), GFP_KERNEL); 217ad6eb31eSJames Morse if (!reg) { 218ad6eb31eSJames Morse kfree(event); 219ad6eb31eSJames Morse return ERR_PTR(-ENOMEM); 220ad6eb31eSJames Morse } 221ad6eb31eSJames Morse 222ad6eb31eSJames Morse reg->event_num = event_num; 223ad6eb31eSJames Morse reg->priority = event->priority; 224ad6eb31eSJames Morse 225ad6eb31eSJames Morse reg->callback = cb; 226ad6eb31eSJames Morse reg->callback_arg = cb_arg; 227ad6eb31eSJames Morse event->registered = reg; 228f92b5462SJames Morse } else { 229f92b5462SJames Morse int cpu; 230f92b5462SJames Morse struct sdei_registered_event __percpu *regs; 231f92b5462SJames Morse 232f92b5462SJames Morse regs = alloc_percpu(struct sdei_registered_event); 233f92b5462SJames Morse if (!regs) { 234f92b5462SJames Morse kfree(event); 235f92b5462SJames Morse return ERR_PTR(-ENOMEM); 236f92b5462SJames Morse } 237f92b5462SJames Morse 238f92b5462SJames Morse for_each_possible_cpu(cpu) { 239f92b5462SJames Morse reg = per_cpu_ptr(regs, cpu); 240f92b5462SJames Morse 241f92b5462SJames Morse reg->event_num = event->event_num; 242f92b5462SJames Morse reg->priority = event->priority; 243f92b5462SJames Morse reg->callback = cb; 244f92b5462SJames Morse reg->callback_arg = cb_arg; 245f92b5462SJames Morse } 246f92b5462SJames Morse 247f92b5462SJames Morse event->private_registered = regs; 248ad6eb31eSJames Morse } 249ad6eb31eSJames Morse 250ad6eb31eSJames Morse spin_lock(&sdei_list_lock); 251ad6eb31eSJames Morse list_add(&event->list, &sdei_list); 252ad6eb31eSJames Morse spin_unlock(&sdei_list_lock); 253ad6eb31eSJames Morse 254ad6eb31eSJames Morse return event; 255ad6eb31eSJames Morse } 256ad6eb31eSJames Morse 257c66d52b1SLiguang Zhang static void sdei_event_destroy_llocked(struct sdei_event *event) 258ad6eb31eSJames Morse { 259ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 260c66d52b1SLiguang Zhang lockdep_assert_held(&sdei_list_lock); 261ad6eb31eSJames Morse 262ad6eb31eSJames Morse list_del(&event->list); 263ad6eb31eSJames Morse 264ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 265ad6eb31eSJames Morse kfree(event->registered); 266f92b5462SJames Morse else 267f92b5462SJames Morse free_percpu(event->private_registered); 268ad6eb31eSJames Morse 269ad6eb31eSJames Morse kfree(event); 270ad6eb31eSJames Morse } 271ad6eb31eSJames Morse 272c66d52b1SLiguang Zhang static void sdei_event_destroy(struct sdei_event *event) 273c66d52b1SLiguang Zhang { 274c66d52b1SLiguang Zhang spin_lock(&sdei_list_lock); 275c66d52b1SLiguang Zhang sdei_event_destroy_llocked(event); 276c66d52b1SLiguang Zhang spin_unlock(&sdei_list_lock); 277c66d52b1SLiguang Zhang } 278c66d52b1SLiguang Zhang 279ad6eb31eSJames Morse static int sdei_api_get_version(u64 *version) 280ad6eb31eSJames Morse { 281ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_VERSION, 0, 0, 0, 0, 0, version); 282ad6eb31eSJames Morse } 283ad6eb31eSJames Morse 284ad6eb31eSJames Morse int sdei_mask_local_cpu(void) 285ad6eb31eSJames Morse { 286ad6eb31eSJames Morse int err; 287ad6eb31eSJames Morse 288ad6eb31eSJames Morse WARN_ON_ONCE(preemptible()); 289ad6eb31eSJames Morse 290ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_MASK, 0, 0, 0, 0, 0, NULL); 291ad6eb31eSJames Morse if (err && err != -EIO) { 292ad6eb31eSJames Morse pr_warn_once("failed to mask CPU[%u]: %d\n", 293ad6eb31eSJames Morse smp_processor_id(), err); 294ad6eb31eSJames Morse return err; 295ad6eb31eSJames Morse } 296ad6eb31eSJames Morse 297ad6eb31eSJames Morse return 0; 298ad6eb31eSJames Morse } 299ad6eb31eSJames Morse 300ad6eb31eSJames Morse static void _ipi_mask_cpu(void *ignored) 301ad6eb31eSJames Morse { 302ad6eb31eSJames Morse sdei_mask_local_cpu(); 303ad6eb31eSJames Morse } 304ad6eb31eSJames Morse 305ad6eb31eSJames Morse int sdei_unmask_local_cpu(void) 306ad6eb31eSJames Morse { 307ad6eb31eSJames Morse int err; 308ad6eb31eSJames Morse 309ad6eb31eSJames Morse WARN_ON_ONCE(preemptible()); 310ad6eb31eSJames Morse 311ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_UNMASK, 0, 0, 0, 0, 0, NULL); 312ad6eb31eSJames Morse if (err && err != -EIO) { 313ad6eb31eSJames Morse pr_warn_once("failed to unmask CPU[%u]: %d\n", 314ad6eb31eSJames Morse smp_processor_id(), err); 315ad6eb31eSJames Morse return err; 316ad6eb31eSJames Morse } 317ad6eb31eSJames Morse 318ad6eb31eSJames Morse return 0; 319ad6eb31eSJames Morse } 320ad6eb31eSJames Morse 321ad6eb31eSJames Morse static void _ipi_unmask_cpu(void *ignored) 322ad6eb31eSJames Morse { 323ad6eb31eSJames Morse sdei_unmask_local_cpu(); 324ad6eb31eSJames Morse } 325ad6eb31eSJames Morse 326ad6eb31eSJames Morse static void _ipi_private_reset(void *ignored) 327ad6eb31eSJames Morse { 328ad6eb31eSJames Morse int err; 329ad6eb31eSJames Morse 330ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PRIVATE_RESET, 0, 0, 0, 0, 0, 331ad6eb31eSJames Morse NULL); 332ad6eb31eSJames Morse if (err && err != -EIO) 333ad6eb31eSJames Morse pr_warn_once("failed to reset CPU[%u]: %d\n", 334ad6eb31eSJames Morse smp_processor_id(), err); 335ad6eb31eSJames Morse } 336ad6eb31eSJames Morse 337ad6eb31eSJames Morse static int sdei_api_shared_reset(void) 338ad6eb31eSJames Morse { 339ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_SHARED_RESET, 0, 0, 0, 0, 0, 340ad6eb31eSJames Morse NULL); 341ad6eb31eSJames Morse } 342ad6eb31eSJames Morse 343ad6eb31eSJames Morse static void sdei_mark_interface_broken(void) 344ad6eb31eSJames Morse { 345ad6eb31eSJames Morse pr_err("disabling SDEI firmware interface\n"); 346ad6eb31eSJames Morse on_each_cpu(&_ipi_mask_cpu, NULL, true); 347ad6eb31eSJames Morse sdei_firmware_call = NULL; 348ad6eb31eSJames Morse } 349ad6eb31eSJames Morse 350ad6eb31eSJames Morse static int sdei_platform_reset(void) 351ad6eb31eSJames Morse { 352ad6eb31eSJames Morse int err; 353ad6eb31eSJames Morse 354ad6eb31eSJames Morse on_each_cpu(&_ipi_private_reset, NULL, true); 355ad6eb31eSJames Morse err = sdei_api_shared_reset(); 356ad6eb31eSJames Morse if (err) { 357ad6eb31eSJames Morse pr_err("Failed to reset platform: %d\n", err); 358ad6eb31eSJames Morse sdei_mark_interface_broken(); 359ad6eb31eSJames Morse } 360ad6eb31eSJames Morse 361ad6eb31eSJames Morse return err; 362ad6eb31eSJames Morse } 363ad6eb31eSJames Morse 364ad6eb31eSJames Morse static int sdei_api_event_enable(u32 event_num) 365ad6eb31eSJames Morse { 366ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_ENABLE, event_num, 0, 0, 0, 367ad6eb31eSJames Morse 0, NULL); 368ad6eb31eSJames Morse } 369ad6eb31eSJames Morse 370f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 371f92b5462SJames Morse static void _local_event_enable(void *data) 372f92b5462SJames Morse { 373f92b5462SJames Morse int err; 374f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 375f92b5462SJames Morse 376f92b5462SJames Morse WARN_ON_ONCE(preemptible()); 377f92b5462SJames Morse 378f92b5462SJames Morse err = sdei_api_event_enable(arg->event->event_num); 379f92b5462SJames Morse 380f92b5462SJames Morse sdei_cross_call_return(arg, err); 381f92b5462SJames Morse } 382f92b5462SJames Morse 383ad6eb31eSJames Morse int sdei_event_enable(u32 event_num) 384ad6eb31eSJames Morse { 385ad6eb31eSJames Morse int err = -EINVAL; 386ad6eb31eSJames Morse struct sdei_event *event; 387ad6eb31eSJames Morse 388ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 389ad6eb31eSJames Morse event = sdei_event_find(event_num); 390ad6eb31eSJames Morse if (!event) { 391ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 392ad6eb31eSJames Morse return -ENOENT; 393ad6eb31eSJames Morse } 394ad6eb31eSJames Morse 395da351827SJames Morse 39654f529a6SJames Morse cpus_read_lock(); 397ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 398ad6eb31eSJames Morse err = sdei_api_event_enable(event->event_num); 399f92b5462SJames Morse else 400f92b5462SJames Morse err = sdei_do_cross_call(_local_event_enable, event); 40154f529a6SJames Morse 40254f529a6SJames Morse if (!err) { 40354f529a6SJames Morse spin_lock(&sdei_list_lock); 40454f529a6SJames Morse event->reenable = true; 40554f529a6SJames Morse spin_unlock(&sdei_list_lock); 40654f529a6SJames Morse } 40754f529a6SJames Morse cpus_read_unlock(); 408ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 409ad6eb31eSJames Morse 410ad6eb31eSJames Morse return err; 411ad6eb31eSJames Morse } 412ad6eb31eSJames Morse 413ad6eb31eSJames Morse static int sdei_api_event_disable(u32 event_num) 414ad6eb31eSJames Morse { 415ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_DISABLE, event_num, 0, 0, 416ad6eb31eSJames Morse 0, 0, NULL); 417ad6eb31eSJames Morse } 418ad6eb31eSJames Morse 419f92b5462SJames Morse static void _ipi_event_disable(void *data) 420f92b5462SJames Morse { 421f92b5462SJames Morse int err; 422f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 423f92b5462SJames Morse 424f92b5462SJames Morse err = sdei_api_event_disable(arg->event->event_num); 425f92b5462SJames Morse 426f92b5462SJames Morse sdei_cross_call_return(arg, err); 427f92b5462SJames Morse } 428f92b5462SJames Morse 429ad6eb31eSJames Morse int sdei_event_disable(u32 event_num) 430ad6eb31eSJames Morse { 431ad6eb31eSJames Morse int err = -EINVAL; 432ad6eb31eSJames Morse struct sdei_event *event; 433ad6eb31eSJames Morse 434ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 435ad6eb31eSJames Morse event = sdei_event_find(event_num); 436ad6eb31eSJames Morse if (!event) { 437ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 438ad6eb31eSJames Morse return -ENOENT; 439ad6eb31eSJames Morse } 440ad6eb31eSJames Morse 441da351827SJames Morse spin_lock(&sdei_list_lock); 442da351827SJames Morse event->reenable = false; 443da351827SJames Morse spin_unlock(&sdei_list_lock); 444da351827SJames Morse 445ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 446ad6eb31eSJames Morse err = sdei_api_event_disable(event->event_num); 447f92b5462SJames Morse else 448f92b5462SJames Morse err = sdei_do_cross_call(_ipi_event_disable, event); 449ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 450ad6eb31eSJames Morse 451ad6eb31eSJames Morse return err; 452ad6eb31eSJames Morse } 453ad6eb31eSJames Morse 454ad6eb31eSJames Morse static int sdei_api_event_unregister(u32 event_num) 455ad6eb31eSJames Morse { 456ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_UNREGISTER, event_num, 0, 457ad6eb31eSJames Morse 0, 0, 0, NULL); 458ad6eb31eSJames Morse } 459ad6eb31eSJames Morse 460f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 461f92b5462SJames Morse static void _local_event_unregister(void *data) 462f92b5462SJames Morse { 463f92b5462SJames Morse int err; 464f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 465f92b5462SJames Morse 466f92b5462SJames Morse WARN_ON_ONCE(preemptible()); 467f92b5462SJames Morse 468f92b5462SJames Morse err = sdei_api_event_unregister(arg->event->event_num); 469f92b5462SJames Morse 470f92b5462SJames Morse sdei_cross_call_return(arg, err); 471f92b5462SJames Morse } 472f92b5462SJames Morse 473ad6eb31eSJames Morse static int _sdei_event_unregister(struct sdei_event *event) 474ad6eb31eSJames Morse { 475ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 476ad6eb31eSJames Morse 477ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 478ad6eb31eSJames Morse return sdei_api_event_unregister(event->event_num); 479ad6eb31eSJames Morse 480f92b5462SJames Morse return sdei_do_cross_call(_local_event_unregister, event); 481ad6eb31eSJames Morse } 482ad6eb31eSJames Morse 483ad6eb31eSJames Morse int sdei_event_unregister(u32 event_num) 484ad6eb31eSJames Morse { 485ad6eb31eSJames Morse int err; 486ad6eb31eSJames Morse struct sdei_event *event; 487ad6eb31eSJames Morse 488ad6eb31eSJames Morse WARN_ON(in_nmi()); 489ad6eb31eSJames Morse 490ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 491ad6eb31eSJames Morse event = sdei_event_find(event_num); 492ad6eb31eSJames Morse do { 493ad6eb31eSJames Morse if (!event) { 494ad6eb31eSJames Morse pr_warn("Event %u not registered\n", event_num); 495ad6eb31eSJames Morse err = -ENOENT; 496ad6eb31eSJames Morse break; 497ad6eb31eSJames Morse } 498ad6eb31eSJames Morse 4996ded0b61SJames Morse spin_lock(&sdei_list_lock); 5006ded0b61SJames Morse event->reregister = false; 5016ded0b61SJames Morse event->reenable = false; 5026ded0b61SJames Morse spin_unlock(&sdei_list_lock); 5036ded0b61SJames Morse 504ad6eb31eSJames Morse err = _sdei_event_unregister(event); 505ad6eb31eSJames Morse if (err) 506ad6eb31eSJames Morse break; 507ad6eb31eSJames Morse 508ad6eb31eSJames Morse sdei_event_destroy(event); 509ad6eb31eSJames Morse } while (0); 510ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 511ad6eb31eSJames Morse 512ad6eb31eSJames Morse return err; 513ad6eb31eSJames Morse } 514ad6eb31eSJames Morse 515da351827SJames Morse /* 516da351827SJames Morse * unregister events, but don't destroy them as they are re-registered by 517da351827SJames Morse * sdei_reregister_shared(). 518da351827SJames Morse */ 519da351827SJames Morse static int sdei_unregister_shared(void) 520da351827SJames Morse { 521da351827SJames Morse int err = 0; 522da351827SJames Morse struct sdei_event *event; 523da351827SJames Morse 524da351827SJames Morse mutex_lock(&sdei_events_lock); 525da351827SJames Morse spin_lock(&sdei_list_lock); 526da351827SJames Morse list_for_each_entry(event, &sdei_list, list) { 527da351827SJames Morse if (event->type != SDEI_EVENT_TYPE_SHARED) 528da351827SJames Morse continue; 529da351827SJames Morse 530da351827SJames Morse err = _sdei_event_unregister(event); 531da351827SJames Morse if (err) 532da351827SJames Morse break; 533da351827SJames Morse } 534da351827SJames Morse spin_unlock(&sdei_list_lock); 535da351827SJames Morse mutex_unlock(&sdei_events_lock); 536da351827SJames Morse 537da351827SJames Morse return err; 538da351827SJames Morse } 539da351827SJames Morse 540ad6eb31eSJames Morse static int sdei_api_event_register(u32 event_num, unsigned long entry_point, 541ad6eb31eSJames Morse void *arg, u64 flags, u64 affinity) 542ad6eb31eSJames Morse { 543ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_REGISTER, event_num, 544ad6eb31eSJames Morse (unsigned long)entry_point, (unsigned long)arg, 545ad6eb31eSJames Morse flags, affinity, NULL); 546ad6eb31eSJames Morse } 547ad6eb31eSJames Morse 548f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 549f92b5462SJames Morse static void _local_event_register(void *data) 550f92b5462SJames Morse { 551f92b5462SJames Morse int err; 552f92b5462SJames Morse struct sdei_registered_event *reg; 553f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 554f92b5462SJames Morse 555f92b5462SJames Morse WARN_ON(preemptible()); 556f92b5462SJames Morse 557f92b5462SJames Morse reg = per_cpu_ptr(arg->event->private_registered, smp_processor_id()); 558f92b5462SJames Morse err = sdei_api_event_register(arg->event->event_num, sdei_entry_point, 559f92b5462SJames Morse reg, 0, 0); 560f92b5462SJames Morse 561f92b5462SJames Morse sdei_cross_call_return(arg, err); 562f92b5462SJames Morse } 563f92b5462SJames Morse 564ad6eb31eSJames Morse static int _sdei_event_register(struct sdei_event *event) 565ad6eb31eSJames Morse { 566f92b5462SJames Morse int err; 567f92b5462SJames Morse 568ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 569ad6eb31eSJames Morse 570ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 571ad6eb31eSJames Morse return sdei_api_event_register(event->event_num, 572ad6eb31eSJames Morse sdei_entry_point, 573ad6eb31eSJames Morse event->registered, 574ad6eb31eSJames Morse SDEI_EVENT_REGISTER_RM_ANY, 0); 575ad6eb31eSJames Morse 576f92b5462SJames Morse err = sdei_do_cross_call(_local_event_register, event); 5776ded0b61SJames Morse if (err) 578f92b5462SJames Morse sdei_do_cross_call(_local_event_unregister, event); 579f92b5462SJames Morse 580f92b5462SJames Morse return err; 581ad6eb31eSJames Morse } 582ad6eb31eSJames Morse 583ad6eb31eSJames Morse int sdei_event_register(u32 event_num, sdei_event_callback *cb, void *arg) 584ad6eb31eSJames Morse { 585ad6eb31eSJames Morse int err; 586ad6eb31eSJames Morse struct sdei_event *event; 587ad6eb31eSJames Morse 588ad6eb31eSJames Morse WARN_ON(in_nmi()); 589ad6eb31eSJames Morse 590ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 591ad6eb31eSJames Morse do { 592ad6eb31eSJames Morse if (sdei_event_find(event_num)) { 593ad6eb31eSJames Morse pr_warn("Event %u already registered\n", event_num); 594ad6eb31eSJames Morse err = -EBUSY; 595ad6eb31eSJames Morse break; 596ad6eb31eSJames Morse } 597ad6eb31eSJames Morse 598ad6eb31eSJames Morse event = sdei_event_create(event_num, cb, arg); 599ad6eb31eSJames Morse if (IS_ERR(event)) { 600ad6eb31eSJames Morse err = PTR_ERR(event); 601ad6eb31eSJames Morse pr_warn("Failed to create event %u: %d\n", event_num, 602ad6eb31eSJames Morse err); 603ad6eb31eSJames Morse break; 604ad6eb31eSJames Morse } 605ad6eb31eSJames Morse 60654f529a6SJames Morse cpus_read_lock(); 607ad6eb31eSJames Morse err = _sdei_event_register(event); 608ad6eb31eSJames Morse if (err) { 609ad6eb31eSJames Morse sdei_event_destroy(event); 610ad6eb31eSJames Morse pr_warn("Failed to register event %u: %d\n", event_num, 611ad6eb31eSJames Morse err); 61254f529a6SJames Morse } else { 61354f529a6SJames Morse spin_lock(&sdei_list_lock); 61454f529a6SJames Morse event->reregister = true; 61554f529a6SJames Morse spin_unlock(&sdei_list_lock); 616ad6eb31eSJames Morse } 61754f529a6SJames Morse cpus_read_unlock(); 618ad6eb31eSJames Morse } while (0); 619ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 620ad6eb31eSJames Morse 621ad6eb31eSJames Morse return err; 622ad6eb31eSJames Morse } 623ad6eb31eSJames Morse 624c66d52b1SLiguang Zhang static int sdei_reregister_event_llocked(struct sdei_event *event) 625da351827SJames Morse { 626da351827SJames Morse int err; 627da351827SJames Morse 628da351827SJames Morse lockdep_assert_held(&sdei_events_lock); 629c66d52b1SLiguang Zhang lockdep_assert_held(&sdei_list_lock); 630da351827SJames Morse 631da351827SJames Morse err = _sdei_event_register(event); 632da351827SJames Morse if (err) { 633da351827SJames Morse pr_err("Failed to re-register event %u\n", event->event_num); 634c66d52b1SLiguang Zhang sdei_event_destroy_llocked(event); 635da351827SJames Morse return err; 636da351827SJames Morse } 637da351827SJames Morse 638da351827SJames Morse if (event->reenable) { 639da351827SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 640da351827SJames Morse err = sdei_api_event_enable(event->event_num); 641f92b5462SJames Morse else 642f92b5462SJames Morse err = sdei_do_cross_call(_local_event_enable, event); 643da351827SJames Morse } 644da351827SJames Morse 645da351827SJames Morse if (err) 646da351827SJames Morse pr_err("Failed to re-enable event %u\n", event->event_num); 647da351827SJames Morse 648da351827SJames Morse return err; 649da351827SJames Morse } 650da351827SJames Morse 651da351827SJames Morse static int sdei_reregister_shared(void) 652da351827SJames Morse { 653da351827SJames Morse int err = 0; 654da351827SJames Morse struct sdei_event *event; 655da351827SJames Morse 656da351827SJames Morse mutex_lock(&sdei_events_lock); 657da351827SJames Morse spin_lock(&sdei_list_lock); 658da351827SJames Morse list_for_each_entry(event, &sdei_list, list) { 659da351827SJames Morse if (event->type != SDEI_EVENT_TYPE_SHARED) 660da351827SJames Morse continue; 661da351827SJames Morse 662da351827SJames Morse if (event->reregister) { 663c66d52b1SLiguang Zhang err = sdei_reregister_event_llocked(event); 664da351827SJames Morse if (err) 665da351827SJames Morse break; 666da351827SJames Morse } 667da351827SJames Morse } 668da351827SJames Morse spin_unlock(&sdei_list_lock); 669da351827SJames Morse mutex_unlock(&sdei_events_lock); 670da351827SJames Morse 671da351827SJames Morse return err; 672da351827SJames Morse } 673da351827SJames Morse 674f92b5462SJames Morse static int sdei_cpuhp_down(unsigned int cpu) 675f92b5462SJames Morse { 676f92b5462SJames Morse struct sdei_event *event; 677f92b5462SJames Morse struct sdei_crosscall_args arg; 678f92b5462SJames Morse 679f92b5462SJames Morse /* un-register private events */ 680f92b5462SJames Morse spin_lock(&sdei_list_lock); 681f92b5462SJames Morse list_for_each_entry(event, &sdei_list, list) { 682f92b5462SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 683f92b5462SJames Morse continue; 684f92b5462SJames Morse 685f92b5462SJames Morse CROSSCALL_INIT(arg, event); 686f92b5462SJames Morse /* call the cross-call function locally... */ 687f92b5462SJames Morse _local_event_unregister(&arg); 688f92b5462SJames Morse if (arg.first_error) 689f92b5462SJames Morse pr_err("Failed to unregister event %u: %d\n", 690f92b5462SJames Morse event->event_num, arg.first_error); 691f92b5462SJames Morse } 692f92b5462SJames Morse spin_unlock(&sdei_list_lock); 693f92b5462SJames Morse 694f92b5462SJames Morse return sdei_mask_local_cpu(); 695f92b5462SJames Morse } 696f92b5462SJames Morse 697f92b5462SJames Morse static int sdei_cpuhp_up(unsigned int cpu) 698f92b5462SJames Morse { 699f92b5462SJames Morse struct sdei_event *event; 700f92b5462SJames Morse struct sdei_crosscall_args arg; 701f92b5462SJames Morse 702f92b5462SJames Morse /* re-register/enable private events */ 703f92b5462SJames Morse spin_lock(&sdei_list_lock); 704f92b5462SJames Morse list_for_each_entry(event, &sdei_list, list) { 705f92b5462SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 706f92b5462SJames Morse continue; 707f92b5462SJames Morse 708f92b5462SJames Morse if (event->reregister) { 709f92b5462SJames Morse CROSSCALL_INIT(arg, event); 710f92b5462SJames Morse /* call the cross-call function locally... */ 711f92b5462SJames Morse _local_event_register(&arg); 712f92b5462SJames Morse if (arg.first_error) 713f92b5462SJames Morse pr_err("Failed to re-register event %u: %d\n", 714f92b5462SJames Morse event->event_num, arg.first_error); 715f92b5462SJames Morse } 716f92b5462SJames Morse 717f92b5462SJames Morse if (event->reenable) { 718f92b5462SJames Morse CROSSCALL_INIT(arg, event); 719f92b5462SJames Morse _local_event_enable(&arg); 720f92b5462SJames Morse if (arg.first_error) 721f92b5462SJames Morse pr_err("Failed to re-enable event %u: %d\n", 722f92b5462SJames Morse event->event_num, arg.first_error); 723f92b5462SJames Morse } 724f92b5462SJames Morse } 725f92b5462SJames Morse spin_unlock(&sdei_list_lock); 726f92b5462SJames Morse 727f92b5462SJames Morse return sdei_unmask_local_cpu(); 728f92b5462SJames Morse } 729f92b5462SJames Morse 730da351827SJames Morse /* When entering idle, mask/unmask events for this cpu */ 731da351827SJames Morse static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action, 732da351827SJames Morse void *data) 733da351827SJames Morse { 734da351827SJames Morse int rv; 735da351827SJames Morse 736da351827SJames Morse switch (action) { 737da351827SJames Morse case CPU_PM_ENTER: 738da351827SJames Morse rv = sdei_mask_local_cpu(); 739da351827SJames Morse break; 740da351827SJames Morse case CPU_PM_EXIT: 741da351827SJames Morse case CPU_PM_ENTER_FAILED: 742da351827SJames Morse rv = sdei_unmask_local_cpu(); 743da351827SJames Morse break; 744da351827SJames Morse default: 745da351827SJames Morse return NOTIFY_DONE; 746da351827SJames Morse } 747da351827SJames Morse 748da351827SJames Morse if (rv) 749da351827SJames Morse return notifier_from_errno(rv); 750da351827SJames Morse 751da351827SJames Morse return NOTIFY_OK; 752da351827SJames Morse } 753da351827SJames Morse 754da351827SJames Morse static struct notifier_block sdei_pm_nb = { 755da351827SJames Morse .notifier_call = sdei_pm_notifier, 756da351827SJames Morse }; 757da351827SJames Morse 758da351827SJames Morse static int sdei_device_suspend(struct device *dev) 759da351827SJames Morse { 760da351827SJames Morse on_each_cpu(_ipi_mask_cpu, NULL, true); 761da351827SJames Morse 762da351827SJames Morse return 0; 763da351827SJames Morse } 764da351827SJames Morse 765da351827SJames Morse static int sdei_device_resume(struct device *dev) 766da351827SJames Morse { 767da351827SJames Morse on_each_cpu(_ipi_unmask_cpu, NULL, true); 768da351827SJames Morse 769da351827SJames Morse return 0; 770da351827SJames Morse } 771da351827SJames Morse 772da351827SJames Morse /* 773da351827SJames Morse * We need all events to be reregistered when we resume from hibernate. 774da351827SJames Morse * 775da351827SJames Morse * The sequence is freeze->thaw. Reboot. freeze->restore. We unregister 776da351827SJames Morse * events during freeze, then re-register and re-enable them during thaw 777da351827SJames Morse * and restore. 778da351827SJames Morse */ 779da351827SJames Morse static int sdei_device_freeze(struct device *dev) 780da351827SJames Morse { 781da351827SJames Morse int err; 782da351827SJames Morse 783f92b5462SJames Morse /* unregister private events */ 784da351827SJames Morse cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); 785da351827SJames Morse 786da351827SJames Morse err = sdei_unregister_shared(); 787da351827SJames Morse if (err) 788da351827SJames Morse return err; 789da351827SJames Morse 790da351827SJames Morse return 0; 791da351827SJames Morse } 792da351827SJames Morse 793da351827SJames Morse static int sdei_device_thaw(struct device *dev) 794da351827SJames Morse { 795da351827SJames Morse int err; 796da351827SJames Morse 797da351827SJames Morse /* re-register shared events */ 798da351827SJames Morse err = sdei_reregister_shared(); 799da351827SJames Morse if (err) { 800da351827SJames Morse pr_warn("Failed to re-register shared events...\n"); 801da351827SJames Morse sdei_mark_interface_broken(); 802da351827SJames Morse return err; 803da351827SJames Morse } 804da351827SJames Morse 805da351827SJames Morse err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", 806da351827SJames Morse &sdei_cpuhp_up, &sdei_cpuhp_down); 807da351827SJames Morse if (err) 808da351827SJames Morse pr_warn("Failed to re-register CPU hotplug notifier...\n"); 809da351827SJames Morse 810da351827SJames Morse return err; 811da351827SJames Morse } 812da351827SJames Morse 813da351827SJames Morse static int sdei_device_restore(struct device *dev) 814da351827SJames Morse { 815da351827SJames Morse int err; 816da351827SJames Morse 817da351827SJames Morse err = sdei_platform_reset(); 818da351827SJames Morse if (err) 819da351827SJames Morse return err; 820da351827SJames Morse 821da351827SJames Morse return sdei_device_thaw(dev); 822da351827SJames Morse } 823da351827SJames Morse 824da351827SJames Morse static const struct dev_pm_ops sdei_pm_ops = { 825da351827SJames Morse .suspend = sdei_device_suspend, 826da351827SJames Morse .resume = sdei_device_resume, 827da351827SJames Morse .freeze = sdei_device_freeze, 828da351827SJames Morse .thaw = sdei_device_thaw, 829da351827SJames Morse .restore = sdei_device_restore, 830da351827SJames Morse }; 831da351827SJames Morse 832da351827SJames Morse /* 833da351827SJames Morse * Mask all CPUs and unregister all events on panic, reboot or kexec. 834da351827SJames Morse */ 835da351827SJames Morse static int sdei_reboot_notifier(struct notifier_block *nb, unsigned long action, 836da351827SJames Morse void *data) 837da351827SJames Morse { 838da351827SJames Morse /* 839da351827SJames Morse * We are going to reset the interface, after this there is no point 840da351827SJames Morse * doing work when we take CPUs offline. 841da351827SJames Morse */ 842da351827SJames Morse cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); 843da351827SJames Morse 844da351827SJames Morse sdei_platform_reset(); 845da351827SJames Morse 846da351827SJames Morse return NOTIFY_OK; 847da351827SJames Morse } 848da351827SJames Morse 849da351827SJames Morse static struct notifier_block sdei_reboot_nb = { 850da351827SJames Morse .notifier_call = sdei_reboot_notifier, 851da351827SJames Morse }; 852da351827SJames Morse 853ad6eb31eSJames Morse static void sdei_smccc_smc(unsigned long function_id, 854ad6eb31eSJames Morse unsigned long arg0, unsigned long arg1, 855ad6eb31eSJames Morse unsigned long arg2, unsigned long arg3, 856ad6eb31eSJames Morse unsigned long arg4, struct arm_smccc_res *res) 857ad6eb31eSJames Morse { 858ad6eb31eSJames Morse arm_smccc_smc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); 859ad6eb31eSJames Morse } 8602f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(sdei_smccc_smc); 861ad6eb31eSJames Morse 862ad6eb31eSJames Morse static void sdei_smccc_hvc(unsigned long function_id, 863ad6eb31eSJames Morse unsigned long arg0, unsigned long arg1, 864ad6eb31eSJames Morse unsigned long arg2, unsigned long arg3, 865ad6eb31eSJames Morse unsigned long arg4, struct arm_smccc_res *res) 866ad6eb31eSJames Morse { 867ad6eb31eSJames Morse arm_smccc_hvc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); 868ad6eb31eSJames Morse } 8692f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(sdei_smccc_hvc); 870ad6eb31eSJames Morse 871f96935d3SJames Morse int sdei_register_ghes(struct ghes *ghes, sdei_event_callback *normal_cb, 872f96935d3SJames Morse sdei_event_callback *critical_cb) 873f96935d3SJames Morse { 874f96935d3SJames Morse int err; 875f96935d3SJames Morse u64 result; 876f96935d3SJames Morse u32 event_num; 877f96935d3SJames Morse sdei_event_callback *cb; 878f96935d3SJames Morse 879f96935d3SJames Morse if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES)) 880f96935d3SJames Morse return -EOPNOTSUPP; 881f96935d3SJames Morse 882f96935d3SJames Morse event_num = ghes->generic->notify.vector; 883f96935d3SJames Morse if (event_num == 0) { 884f96935d3SJames Morse /* 885f96935d3SJames Morse * Event 0 is reserved by the specification for 886f96935d3SJames Morse * SDEI_EVENT_SIGNAL. 887f96935d3SJames Morse */ 888f96935d3SJames Morse return -EINVAL; 889f96935d3SJames Morse } 890f96935d3SJames Morse 891f96935d3SJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY, 892f96935d3SJames Morse &result); 893f96935d3SJames Morse if (err) 894f96935d3SJames Morse return err; 895f96935d3SJames Morse 896f96935d3SJames Morse if (result == SDEI_EVENT_PRIORITY_CRITICAL) 897f96935d3SJames Morse cb = critical_cb; 898f96935d3SJames Morse else 899f96935d3SJames Morse cb = normal_cb; 900f96935d3SJames Morse 901f96935d3SJames Morse err = sdei_event_register(event_num, cb, ghes); 902f96935d3SJames Morse if (!err) 903f96935d3SJames Morse err = sdei_event_enable(event_num); 904f96935d3SJames Morse 905f96935d3SJames Morse return err; 906f96935d3SJames Morse } 907f96935d3SJames Morse 908f96935d3SJames Morse int sdei_unregister_ghes(struct ghes *ghes) 909f96935d3SJames Morse { 910f96935d3SJames Morse int i; 911f96935d3SJames Morse int err; 912f96935d3SJames Morse u32 event_num = ghes->generic->notify.vector; 913f96935d3SJames Morse 914f96935d3SJames Morse might_sleep(); 915f96935d3SJames Morse 916f96935d3SJames Morse if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES)) 917f96935d3SJames Morse return -EOPNOTSUPP; 918f96935d3SJames Morse 919f96935d3SJames Morse /* 920f96935d3SJames Morse * The event may be running on another CPU. Disable it 921f96935d3SJames Morse * to stop new events, then try to unregister a few times. 922f96935d3SJames Morse */ 923f96935d3SJames Morse err = sdei_event_disable(event_num); 924f96935d3SJames Morse if (err) 925f96935d3SJames Morse return err; 926f96935d3SJames Morse 927f96935d3SJames Morse for (i = 0; i < 3; i++) { 928f96935d3SJames Morse err = sdei_event_unregister(event_num); 929f96935d3SJames Morse if (err != -EINPROGRESS) 930f96935d3SJames Morse break; 931f96935d3SJames Morse 932f96935d3SJames Morse schedule(); 933f96935d3SJames Morse } 934f96935d3SJames Morse 935f96935d3SJames Morse return err; 936f96935d3SJames Morse } 937f96935d3SJames Morse 938ad6eb31eSJames Morse static int sdei_get_conduit(struct platform_device *pdev) 939ad6eb31eSJames Morse { 940ad6eb31eSJames Morse const char *method; 941ad6eb31eSJames Morse struct device_node *np = pdev->dev.of_node; 942ad6eb31eSJames Morse 943ad6eb31eSJames Morse sdei_firmware_call = NULL; 944ad6eb31eSJames Morse if (np) { 945ad6eb31eSJames Morse if (of_property_read_string(np, "method", &method)) { 946ad6eb31eSJames Morse pr_warn("missing \"method\" property\n"); 947e6ea4651SMark Rutland return SMCCC_CONDUIT_NONE; 948ad6eb31eSJames Morse } 949ad6eb31eSJames Morse 950ad6eb31eSJames Morse if (!strcmp("hvc", method)) { 951ad6eb31eSJames Morse sdei_firmware_call = &sdei_smccc_hvc; 952e6ea4651SMark Rutland return SMCCC_CONDUIT_HVC; 953ad6eb31eSJames Morse } else if (!strcmp("smc", method)) { 954ad6eb31eSJames Morse sdei_firmware_call = &sdei_smccc_smc; 955e6ea4651SMark Rutland return SMCCC_CONDUIT_SMC; 956ad6eb31eSJames Morse } 957ad6eb31eSJames Morse 958ad6eb31eSJames Morse pr_warn("invalid \"method\" property: %s\n", method); 959677a60bdSJames Morse } else if (IS_ENABLED(CONFIG_ACPI) && !acpi_disabled) { 960677a60bdSJames Morse if (acpi_psci_use_hvc()) { 961677a60bdSJames Morse sdei_firmware_call = &sdei_smccc_hvc; 962e6ea4651SMark Rutland return SMCCC_CONDUIT_HVC; 963677a60bdSJames Morse } else { 964677a60bdSJames Morse sdei_firmware_call = &sdei_smccc_smc; 965e6ea4651SMark Rutland return SMCCC_CONDUIT_SMC; 966677a60bdSJames Morse } 967ad6eb31eSJames Morse } 968ad6eb31eSJames Morse 969e6ea4651SMark Rutland return SMCCC_CONDUIT_NONE; 970ad6eb31eSJames Morse } 971ad6eb31eSJames Morse 972ad6eb31eSJames Morse static int sdei_probe(struct platform_device *pdev) 973ad6eb31eSJames Morse { 974ad6eb31eSJames Morse int err; 975ad6eb31eSJames Morse u64 ver = 0; 976ad6eb31eSJames Morse int conduit; 977ad6eb31eSJames Morse 978ad6eb31eSJames Morse conduit = sdei_get_conduit(pdev); 979ad6eb31eSJames Morse if (!sdei_firmware_call) 980ad6eb31eSJames Morse return 0; 981ad6eb31eSJames Morse 982ad6eb31eSJames Morse err = sdei_api_get_version(&ver); 983ad6eb31eSJames Morse if (err == -EOPNOTSUPP) 984ad6eb31eSJames Morse pr_err("advertised but not implemented in platform firmware\n"); 985ad6eb31eSJames Morse if (err) { 986ad6eb31eSJames Morse pr_err("Failed to get SDEI version: %d\n", err); 987ad6eb31eSJames Morse sdei_mark_interface_broken(); 988ad6eb31eSJames Morse return err; 989ad6eb31eSJames Morse } 990ad6eb31eSJames Morse 991ad6eb31eSJames Morse pr_info("SDEIv%d.%d (0x%x) detected in firmware.\n", 992ad6eb31eSJames Morse (int)SDEI_VERSION_MAJOR(ver), (int)SDEI_VERSION_MINOR(ver), 993ad6eb31eSJames Morse (int)SDEI_VERSION_VENDOR(ver)); 994ad6eb31eSJames Morse 995ad6eb31eSJames Morse if (SDEI_VERSION_MAJOR(ver) != 1) { 996ad6eb31eSJames Morse pr_warn("Conflicting SDEI version detected.\n"); 997ad6eb31eSJames Morse sdei_mark_interface_broken(); 998ad6eb31eSJames Morse return -EINVAL; 999ad6eb31eSJames Morse } 1000ad6eb31eSJames Morse 1001ad6eb31eSJames Morse err = sdei_platform_reset(); 1002ad6eb31eSJames Morse if (err) 1003ad6eb31eSJames Morse return err; 1004ad6eb31eSJames Morse 1005ad6eb31eSJames Morse sdei_entry_point = sdei_arch_get_entry_point(conduit); 1006ad6eb31eSJames Morse if (!sdei_entry_point) { 1007ad6eb31eSJames Morse /* Not supported due to hardware or boot configuration */ 1008ad6eb31eSJames Morse sdei_mark_interface_broken(); 1009ad6eb31eSJames Morse return 0; 1010ad6eb31eSJames Morse } 1011ad6eb31eSJames Morse 1012da351827SJames Morse err = cpu_pm_register_notifier(&sdei_pm_nb); 1013da351827SJames Morse if (err) { 1014da351827SJames Morse pr_warn("Failed to register CPU PM notifier...\n"); 1015da351827SJames Morse goto error; 1016da351827SJames Morse } 1017da351827SJames Morse 1018da351827SJames Morse err = register_reboot_notifier(&sdei_reboot_nb); 1019da351827SJames Morse if (err) { 1020da351827SJames Morse pr_warn("Failed to register reboot notifier...\n"); 1021da351827SJames Morse goto remove_cpupm; 1022da351827SJames Morse } 1023da351827SJames Morse 1024da351827SJames Morse err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", 1025da351827SJames Morse &sdei_cpuhp_up, &sdei_cpuhp_down); 1026da351827SJames Morse if (err) { 1027da351827SJames Morse pr_warn("Failed to register CPU hotplug notifier...\n"); 1028da351827SJames Morse goto remove_reboot; 1029da351827SJames Morse } 1030ad6eb31eSJames Morse 1031ad6eb31eSJames Morse return 0; 1032da351827SJames Morse 1033da351827SJames Morse remove_reboot: 1034da351827SJames Morse unregister_reboot_notifier(&sdei_reboot_nb); 1035da351827SJames Morse 1036da351827SJames Morse remove_cpupm: 1037da351827SJames Morse cpu_pm_unregister_notifier(&sdei_pm_nb); 1038da351827SJames Morse 1039da351827SJames Morse error: 1040da351827SJames Morse sdei_mark_interface_broken(); 1041da351827SJames Morse return err; 1042ad6eb31eSJames Morse } 1043ad6eb31eSJames Morse 1044ad6eb31eSJames Morse static const struct of_device_id sdei_of_match[] = { 1045ad6eb31eSJames Morse { .compatible = "arm,sdei-1.0" }, 1046ad6eb31eSJames Morse {} 1047ad6eb31eSJames Morse }; 1048ad6eb31eSJames Morse 1049ad6eb31eSJames Morse static struct platform_driver sdei_driver = { 1050ad6eb31eSJames Morse .driver = { 1051ad6eb31eSJames Morse .name = "sdei", 1052da351827SJames Morse .pm = &sdei_pm_ops, 1053ad6eb31eSJames Morse .of_match_table = sdei_of_match, 1054ad6eb31eSJames Morse }, 1055ad6eb31eSJames Morse .probe = sdei_probe, 1056ad6eb31eSJames Morse }; 1057ad6eb31eSJames Morse 1058677a60bdSJames Morse static bool __init sdei_present_acpi(void) 1059677a60bdSJames Morse { 1060677a60bdSJames Morse acpi_status status; 1061677a60bdSJames Morse struct acpi_table_header *sdei_table_header; 1062677a60bdSJames Morse 1063677a60bdSJames Morse if (acpi_disabled) 1064677a60bdSJames Morse return false; 1065677a60bdSJames Morse 1066677a60bdSJames Morse status = acpi_get_table(ACPI_SIG_SDEI, 0, &sdei_table_header); 1067677a60bdSJames Morse if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1068677a60bdSJames Morse const char *msg = acpi_format_exception(status); 1069677a60bdSJames Morse 1070677a60bdSJames Morse pr_info("Failed to get ACPI:SDEI table, %s\n", msg); 1071677a60bdSJames Morse } 1072677a60bdSJames Morse if (ACPI_FAILURE(status)) 1073677a60bdSJames Morse return false; 1074677a60bdSJames Morse 107570e6352aSHanjun Guo acpi_put_table(sdei_table_header); 107670e6352aSHanjun Guo 1077677a60bdSJames Morse return true; 1078677a60bdSJames Morse } 1079677a60bdSJames Morse 1080ad6eb31eSJames Morse static int __init sdei_init(void) 1081ad6eb31eSJames Morse { 1082caf2cd61SSudeep Holla int ret = platform_driver_register(&sdei_driver); 1083ad6eb31eSJames Morse 1084caf2cd61SSudeep Holla if (!ret && sdei_present_acpi()) { 1085caf2cd61SSudeep Holla struct platform_device *pdev; 1086caf2cd61SSudeep Holla 1087caf2cd61SSudeep Holla pdev = platform_device_register_simple(sdei_driver.driver.name, 1088caf2cd61SSudeep Holla 0, NULL, 0); 1089caf2cd61SSudeep Holla if (IS_ERR(pdev)) 1090caf2cd61SSudeep Holla pr_info("Failed to register ACPI:SDEI platform device %ld\n", 1091caf2cd61SSudeep Holla PTR_ERR(pdev)); 1092caf2cd61SSudeep Holla } 1093caf2cd61SSudeep Holla 1094caf2cd61SSudeep Holla return ret; 1095ad6eb31eSJames Morse } 1096ad6eb31eSJames Morse 1097677a60bdSJames Morse /* 1098677a60bdSJames Morse * On an ACPI system SDEI needs to be ready before HEST:GHES tries to register 1099677a60bdSJames Morse * its events. ACPI is initialised from a subsys_initcall(), GHES is initialised 1100677a60bdSJames Morse * by device_initcall(). We want to be called in the middle. 1101677a60bdSJames Morse */ 1102ad6eb31eSJames Morse subsys_initcall_sync(sdei_init); 1103ad6eb31eSJames Morse 1104ad6eb31eSJames Morse int sdei_event_handler(struct pt_regs *regs, 1105ad6eb31eSJames Morse struct sdei_registered_event *arg) 1106ad6eb31eSJames Morse { 1107ad6eb31eSJames Morse int err; 1108ad6eb31eSJames Morse mm_segment_t orig_addr_limit; 1109ad6eb31eSJames Morse u32 event_num = arg->event_num; 1110ad6eb31eSJames Morse 1111472de63bSJames Morse /* 1112472de63bSJames Morse * Save restore 'fs'. 1113472de63bSJames Morse * The architecture's entry code save/restores 'fs' when taking an 1114472de63bSJames Morse * exception from the kernel. This ensures addr_limit isn't inherited 1115472de63bSJames Morse * if you interrupted something that allowed the uaccess routines to 1116472de63bSJames Morse * access kernel memory. 1117472de63bSJames Morse * Do the same here because this doesn't come via the same entry code. 1118472de63bSJames Morse */ 11193d13f313SChristoph Hellwig orig_addr_limit = force_uaccess_begin(); 1120ad6eb31eSJames Morse 1121ad6eb31eSJames Morse err = arg->callback(event_num, regs, arg->callback_arg); 1122ad6eb31eSJames Morse if (err) 1123ad6eb31eSJames Morse pr_err_ratelimited("event %u on CPU %u failed with error: %d\n", 1124ad6eb31eSJames Morse event_num, smp_processor_id(), err); 1125ad6eb31eSJames Morse 11263d13f313SChristoph Hellwig force_uaccess_end(orig_addr_limit); 1127ad6eb31eSJames Morse 1128ad6eb31eSJames Morse return err; 1129ad6eb31eSJames Morse } 1130ad6eb31eSJames Morse NOKPROBE_SYMBOL(sdei_event_handler); 1131