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 117ad6eb31eSJames Morse /* Not an error value ... */ 118ad6eb31eSJames Morse return sdei_err; 119ad6eb31eSJames Morse } 120ad6eb31eSJames Morse 121ad6eb31eSJames Morse /* 122ad6eb31eSJames Morse * If x0 is any of these values, then the call failed, use sdei_to_linux_errno() 123ad6eb31eSJames Morse * to translate. 124ad6eb31eSJames Morse */ 125ad6eb31eSJames Morse static int sdei_is_err(struct arm_smccc_res *res) 126ad6eb31eSJames Morse { 127ad6eb31eSJames Morse switch (res->a0) { 128ad6eb31eSJames Morse case SDEI_NOT_SUPPORTED: 129ad6eb31eSJames Morse case SDEI_INVALID_PARAMETERS: 130ad6eb31eSJames Morse case SDEI_DENIED: 131ad6eb31eSJames Morse case SDEI_PENDING: 132ad6eb31eSJames Morse case SDEI_OUT_OF_RESOURCE: 133ad6eb31eSJames Morse return true; 134ad6eb31eSJames Morse } 135ad6eb31eSJames Morse 136ad6eb31eSJames Morse return false; 137ad6eb31eSJames Morse } 138ad6eb31eSJames Morse 139ad6eb31eSJames Morse static int invoke_sdei_fn(unsigned long function_id, unsigned long arg0, 140ad6eb31eSJames Morse unsigned long arg1, unsigned long arg2, 141ad6eb31eSJames Morse unsigned long arg3, unsigned long arg4, 142ad6eb31eSJames Morse u64 *result) 143ad6eb31eSJames Morse { 144ad6eb31eSJames Morse int err = 0; 145ad6eb31eSJames Morse struct arm_smccc_res res; 146ad6eb31eSJames Morse 147ad6eb31eSJames Morse if (sdei_firmware_call) { 148ad6eb31eSJames Morse sdei_firmware_call(function_id, arg0, arg1, arg2, arg3, arg4, 149ad6eb31eSJames Morse &res); 150ad6eb31eSJames Morse if (sdei_is_err(&res)) 151ad6eb31eSJames Morse err = sdei_to_linux_errno(res.a0); 152ad6eb31eSJames Morse } else { 153ad6eb31eSJames Morse /* 154ad6eb31eSJames Morse * !sdei_firmware_call means we failed to probe or called 155ad6eb31eSJames Morse * sdei_mark_interface_broken(). -EIO is not an error returned 156ad6eb31eSJames Morse * by sdei_to_linux_errno() and is used to suppress messages 157ad6eb31eSJames Morse * from this driver. 158ad6eb31eSJames Morse */ 159ad6eb31eSJames Morse err = -EIO; 160ad6eb31eSJames Morse res.a0 = SDEI_NOT_SUPPORTED; 161ad6eb31eSJames Morse } 162ad6eb31eSJames Morse 163ad6eb31eSJames Morse if (result) 164ad6eb31eSJames Morse *result = res.a0; 165ad6eb31eSJames Morse 166ad6eb31eSJames Morse return err; 167ad6eb31eSJames Morse } 1682f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(invoke_sdei_fn); 169ad6eb31eSJames Morse 170ad6eb31eSJames Morse static struct sdei_event *sdei_event_find(u32 event_num) 171ad6eb31eSJames Morse { 172ad6eb31eSJames Morse struct sdei_event *e, *found = NULL; 173ad6eb31eSJames Morse 174ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 175ad6eb31eSJames Morse 176ad6eb31eSJames Morse spin_lock(&sdei_list_lock); 177ad6eb31eSJames Morse list_for_each_entry(e, &sdei_list, list) { 178ad6eb31eSJames Morse if (e->event_num == event_num) { 179ad6eb31eSJames Morse found = e; 180ad6eb31eSJames Morse break; 181ad6eb31eSJames Morse } 182ad6eb31eSJames Morse } 183ad6eb31eSJames Morse spin_unlock(&sdei_list_lock); 184ad6eb31eSJames Morse 185ad6eb31eSJames Morse return found; 186ad6eb31eSJames Morse } 187ad6eb31eSJames Morse 188ad6eb31eSJames Morse int sdei_api_event_context(u32 query, u64 *result) 189ad6eb31eSJames Morse { 190ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, query, 0, 0, 0, 0, 191ad6eb31eSJames Morse result); 192ad6eb31eSJames Morse } 193ad6eb31eSJames Morse NOKPROBE_SYMBOL(sdei_api_event_context); 194ad6eb31eSJames Morse 195ad6eb31eSJames Morse static int sdei_api_event_get_info(u32 event, u32 info, u64 *result) 196ad6eb31eSJames Morse { 197ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_GET_INFO, event, info, 0, 198ad6eb31eSJames Morse 0, 0, result); 199ad6eb31eSJames Morse } 200ad6eb31eSJames Morse 201ad6eb31eSJames Morse static struct sdei_event *sdei_event_create(u32 event_num, 202ad6eb31eSJames Morse sdei_event_callback *cb, 203ad6eb31eSJames Morse void *cb_arg) 204ad6eb31eSJames Morse { 205ad6eb31eSJames Morse int err; 206ad6eb31eSJames Morse u64 result; 207ad6eb31eSJames Morse struct sdei_event *event; 208ad6eb31eSJames Morse struct sdei_registered_event *reg; 209ad6eb31eSJames Morse 210ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 211ad6eb31eSJames Morse 212ad6eb31eSJames Morse event = kzalloc(sizeof(*event), GFP_KERNEL); 213ad6eb31eSJames Morse if (!event) 214ad6eb31eSJames Morse return ERR_PTR(-ENOMEM); 215ad6eb31eSJames Morse 216ad6eb31eSJames Morse INIT_LIST_HEAD(&event->list); 217ad6eb31eSJames Morse event->event_num = event_num; 218ad6eb31eSJames Morse 219ad6eb31eSJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY, 220ad6eb31eSJames Morse &result); 221ad6eb31eSJames Morse if (err) { 222ad6eb31eSJames Morse kfree(event); 223ad6eb31eSJames Morse return ERR_PTR(err); 224ad6eb31eSJames Morse } 225ad6eb31eSJames Morse event->priority = result; 226ad6eb31eSJames Morse 227ad6eb31eSJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_TYPE, 228ad6eb31eSJames Morse &result); 229ad6eb31eSJames Morse if (err) { 230ad6eb31eSJames Morse kfree(event); 231ad6eb31eSJames Morse return ERR_PTR(err); 232ad6eb31eSJames Morse } 233ad6eb31eSJames Morse event->type = result; 234ad6eb31eSJames Morse 235ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) { 236ad6eb31eSJames Morse reg = kzalloc(sizeof(*reg), GFP_KERNEL); 237ad6eb31eSJames Morse if (!reg) { 238ad6eb31eSJames Morse kfree(event); 239ad6eb31eSJames Morse return ERR_PTR(-ENOMEM); 240ad6eb31eSJames Morse } 241ad6eb31eSJames Morse 242ad6eb31eSJames Morse reg->event_num = event_num; 243ad6eb31eSJames Morse reg->priority = event->priority; 244ad6eb31eSJames Morse 245ad6eb31eSJames Morse reg->callback = cb; 246ad6eb31eSJames Morse reg->callback_arg = cb_arg; 247ad6eb31eSJames Morse event->registered = reg; 248f92b5462SJames Morse } else { 249f92b5462SJames Morse int cpu; 250f92b5462SJames Morse struct sdei_registered_event __percpu *regs; 251f92b5462SJames Morse 252f92b5462SJames Morse regs = alloc_percpu(struct sdei_registered_event); 253f92b5462SJames Morse if (!regs) { 254f92b5462SJames Morse kfree(event); 255f92b5462SJames Morse return ERR_PTR(-ENOMEM); 256f92b5462SJames Morse } 257f92b5462SJames Morse 258f92b5462SJames Morse for_each_possible_cpu(cpu) { 259f92b5462SJames Morse reg = per_cpu_ptr(regs, cpu); 260f92b5462SJames Morse 261f92b5462SJames Morse reg->event_num = event->event_num; 262f92b5462SJames Morse reg->priority = event->priority; 263f92b5462SJames Morse reg->callback = cb; 264f92b5462SJames Morse reg->callback_arg = cb_arg; 265f92b5462SJames Morse } 266f92b5462SJames Morse 267f92b5462SJames Morse event->private_registered = regs; 268ad6eb31eSJames Morse } 269ad6eb31eSJames Morse 270ad6eb31eSJames Morse spin_lock(&sdei_list_lock); 271ad6eb31eSJames Morse list_add(&event->list, &sdei_list); 272ad6eb31eSJames Morse spin_unlock(&sdei_list_lock); 273ad6eb31eSJames Morse 274ad6eb31eSJames Morse return event; 275ad6eb31eSJames Morse } 276ad6eb31eSJames Morse 277c66d52b1SLiguang Zhang static void sdei_event_destroy_llocked(struct sdei_event *event) 278ad6eb31eSJames Morse { 279ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 280c66d52b1SLiguang Zhang lockdep_assert_held(&sdei_list_lock); 281ad6eb31eSJames Morse 282ad6eb31eSJames Morse list_del(&event->list); 283ad6eb31eSJames Morse 284ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 285ad6eb31eSJames Morse kfree(event->registered); 286f92b5462SJames Morse else 287f92b5462SJames Morse free_percpu(event->private_registered); 288ad6eb31eSJames Morse 289ad6eb31eSJames Morse kfree(event); 290ad6eb31eSJames Morse } 291ad6eb31eSJames Morse 292c66d52b1SLiguang Zhang static void sdei_event_destroy(struct sdei_event *event) 293c66d52b1SLiguang Zhang { 294c66d52b1SLiguang Zhang spin_lock(&sdei_list_lock); 295c66d52b1SLiguang Zhang sdei_event_destroy_llocked(event); 296c66d52b1SLiguang Zhang spin_unlock(&sdei_list_lock); 297c66d52b1SLiguang Zhang } 298c66d52b1SLiguang Zhang 299ad6eb31eSJames Morse static int sdei_api_get_version(u64 *version) 300ad6eb31eSJames Morse { 301ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_VERSION, 0, 0, 0, 0, 0, version); 302ad6eb31eSJames Morse } 303ad6eb31eSJames Morse 304ad6eb31eSJames Morse int sdei_mask_local_cpu(void) 305ad6eb31eSJames Morse { 306ad6eb31eSJames Morse int err; 307ad6eb31eSJames Morse 308ad6eb31eSJames Morse WARN_ON_ONCE(preemptible()); 309ad6eb31eSJames Morse 310ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_MASK, 0, 0, 0, 0, 0, NULL); 311ad6eb31eSJames Morse if (err && err != -EIO) { 312ad6eb31eSJames Morse pr_warn_once("failed to mask CPU[%u]: %d\n", 313ad6eb31eSJames Morse smp_processor_id(), err); 314ad6eb31eSJames Morse return err; 315ad6eb31eSJames Morse } 316ad6eb31eSJames Morse 317ad6eb31eSJames Morse return 0; 318ad6eb31eSJames Morse } 319ad6eb31eSJames Morse 320ad6eb31eSJames Morse static void _ipi_mask_cpu(void *ignored) 321ad6eb31eSJames Morse { 322ad6eb31eSJames Morse sdei_mask_local_cpu(); 323ad6eb31eSJames Morse } 324ad6eb31eSJames Morse 325ad6eb31eSJames Morse int sdei_unmask_local_cpu(void) 326ad6eb31eSJames Morse { 327ad6eb31eSJames Morse int err; 328ad6eb31eSJames Morse 329ad6eb31eSJames Morse WARN_ON_ONCE(preemptible()); 330ad6eb31eSJames Morse 331ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_UNMASK, 0, 0, 0, 0, 0, NULL); 332ad6eb31eSJames Morse if (err && err != -EIO) { 333ad6eb31eSJames Morse pr_warn_once("failed to unmask CPU[%u]: %d\n", 334ad6eb31eSJames Morse smp_processor_id(), err); 335ad6eb31eSJames Morse return err; 336ad6eb31eSJames Morse } 337ad6eb31eSJames Morse 338ad6eb31eSJames Morse return 0; 339ad6eb31eSJames Morse } 340ad6eb31eSJames Morse 341ad6eb31eSJames Morse static void _ipi_unmask_cpu(void *ignored) 342ad6eb31eSJames Morse { 343ad6eb31eSJames Morse sdei_unmask_local_cpu(); 344ad6eb31eSJames Morse } 345ad6eb31eSJames Morse 346ad6eb31eSJames Morse static void _ipi_private_reset(void *ignored) 347ad6eb31eSJames Morse { 348ad6eb31eSJames Morse int err; 349ad6eb31eSJames Morse 350ad6eb31eSJames Morse err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PRIVATE_RESET, 0, 0, 0, 0, 0, 351ad6eb31eSJames Morse NULL); 352ad6eb31eSJames Morse if (err && err != -EIO) 353ad6eb31eSJames Morse pr_warn_once("failed to reset CPU[%u]: %d\n", 354ad6eb31eSJames Morse smp_processor_id(), err); 355ad6eb31eSJames Morse } 356ad6eb31eSJames Morse 357ad6eb31eSJames Morse static int sdei_api_shared_reset(void) 358ad6eb31eSJames Morse { 359ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_SHARED_RESET, 0, 0, 0, 0, 0, 360ad6eb31eSJames Morse NULL); 361ad6eb31eSJames Morse } 362ad6eb31eSJames Morse 363ad6eb31eSJames Morse static void sdei_mark_interface_broken(void) 364ad6eb31eSJames Morse { 365ad6eb31eSJames Morse pr_err("disabling SDEI firmware interface\n"); 366ad6eb31eSJames Morse on_each_cpu(&_ipi_mask_cpu, NULL, true); 367ad6eb31eSJames Morse sdei_firmware_call = NULL; 368ad6eb31eSJames Morse } 369ad6eb31eSJames Morse 370ad6eb31eSJames Morse static int sdei_platform_reset(void) 371ad6eb31eSJames Morse { 372ad6eb31eSJames Morse int err; 373ad6eb31eSJames Morse 374ad6eb31eSJames Morse on_each_cpu(&_ipi_private_reset, NULL, true); 375ad6eb31eSJames Morse err = sdei_api_shared_reset(); 376ad6eb31eSJames Morse if (err) { 377ad6eb31eSJames Morse pr_err("Failed to reset platform: %d\n", err); 378ad6eb31eSJames Morse sdei_mark_interface_broken(); 379ad6eb31eSJames Morse } 380ad6eb31eSJames Morse 381ad6eb31eSJames Morse return err; 382ad6eb31eSJames Morse } 383ad6eb31eSJames Morse 384ad6eb31eSJames Morse static int sdei_api_event_enable(u32 event_num) 385ad6eb31eSJames Morse { 386ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_ENABLE, event_num, 0, 0, 0, 387ad6eb31eSJames Morse 0, NULL); 388ad6eb31eSJames Morse } 389ad6eb31eSJames Morse 390f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 391f92b5462SJames Morse static void _local_event_enable(void *data) 392f92b5462SJames Morse { 393f92b5462SJames Morse int err; 394f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 395f92b5462SJames Morse 396f92b5462SJames Morse WARN_ON_ONCE(preemptible()); 397f92b5462SJames Morse 398f92b5462SJames Morse err = sdei_api_event_enable(arg->event->event_num); 399f92b5462SJames Morse 400f92b5462SJames Morse sdei_cross_call_return(arg, err); 401f92b5462SJames Morse } 402f92b5462SJames Morse 403ad6eb31eSJames Morse int sdei_event_enable(u32 event_num) 404ad6eb31eSJames Morse { 405ad6eb31eSJames Morse int err = -EINVAL; 406ad6eb31eSJames Morse struct sdei_event *event; 407ad6eb31eSJames Morse 408ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 409ad6eb31eSJames Morse event = sdei_event_find(event_num); 410ad6eb31eSJames Morse if (!event) { 411ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 412ad6eb31eSJames Morse return -ENOENT; 413ad6eb31eSJames Morse } 414ad6eb31eSJames Morse 415da351827SJames Morse 41654f529a6SJames Morse cpus_read_lock(); 417ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 418ad6eb31eSJames Morse err = sdei_api_event_enable(event->event_num); 419f92b5462SJames Morse else 420f92b5462SJames Morse err = sdei_do_cross_call(_local_event_enable, event); 42154f529a6SJames Morse 42254f529a6SJames Morse if (!err) { 42354f529a6SJames Morse spin_lock(&sdei_list_lock); 42454f529a6SJames Morse event->reenable = true; 42554f529a6SJames Morse spin_unlock(&sdei_list_lock); 42654f529a6SJames Morse } 42754f529a6SJames Morse cpus_read_unlock(); 428ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 429ad6eb31eSJames Morse 430ad6eb31eSJames Morse return err; 431ad6eb31eSJames Morse } 432ad6eb31eSJames Morse 433ad6eb31eSJames Morse static int sdei_api_event_disable(u32 event_num) 434ad6eb31eSJames Morse { 435ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_DISABLE, event_num, 0, 0, 436ad6eb31eSJames Morse 0, 0, NULL); 437ad6eb31eSJames Morse } 438ad6eb31eSJames Morse 439f92b5462SJames Morse static void _ipi_event_disable(void *data) 440f92b5462SJames Morse { 441f92b5462SJames Morse int err; 442f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 443f92b5462SJames Morse 444f92b5462SJames Morse err = sdei_api_event_disable(arg->event->event_num); 445f92b5462SJames Morse 446f92b5462SJames Morse sdei_cross_call_return(arg, err); 447f92b5462SJames Morse } 448f92b5462SJames Morse 449ad6eb31eSJames Morse int sdei_event_disable(u32 event_num) 450ad6eb31eSJames Morse { 451ad6eb31eSJames Morse int err = -EINVAL; 452ad6eb31eSJames Morse struct sdei_event *event; 453ad6eb31eSJames Morse 454ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 455ad6eb31eSJames Morse event = sdei_event_find(event_num); 456ad6eb31eSJames Morse if (!event) { 457ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 458ad6eb31eSJames Morse return -ENOENT; 459ad6eb31eSJames Morse } 460ad6eb31eSJames Morse 461da351827SJames Morse spin_lock(&sdei_list_lock); 462da351827SJames Morse event->reenable = false; 463da351827SJames Morse spin_unlock(&sdei_list_lock); 464da351827SJames Morse 465ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 466ad6eb31eSJames Morse err = sdei_api_event_disable(event->event_num); 467f92b5462SJames Morse else 468f92b5462SJames Morse err = sdei_do_cross_call(_ipi_event_disable, event); 469ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 470ad6eb31eSJames Morse 471ad6eb31eSJames Morse return err; 472ad6eb31eSJames Morse } 473ad6eb31eSJames Morse 474ad6eb31eSJames Morse static int sdei_api_event_unregister(u32 event_num) 475ad6eb31eSJames Morse { 476ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_UNREGISTER, event_num, 0, 477ad6eb31eSJames Morse 0, 0, 0, NULL); 478ad6eb31eSJames Morse } 479ad6eb31eSJames Morse 480f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 481f92b5462SJames Morse static void _local_event_unregister(void *data) 482f92b5462SJames Morse { 483f92b5462SJames Morse int err; 484f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 485f92b5462SJames Morse 486f92b5462SJames Morse WARN_ON_ONCE(preemptible()); 487f92b5462SJames Morse 488f92b5462SJames Morse err = sdei_api_event_unregister(arg->event->event_num); 489f92b5462SJames Morse 490f92b5462SJames Morse sdei_cross_call_return(arg, err); 491f92b5462SJames Morse } 492f92b5462SJames Morse 493ad6eb31eSJames Morse static int _sdei_event_unregister(struct sdei_event *event) 494ad6eb31eSJames Morse { 495ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 496ad6eb31eSJames Morse 497ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 498ad6eb31eSJames Morse return sdei_api_event_unregister(event->event_num); 499ad6eb31eSJames Morse 500f92b5462SJames Morse return sdei_do_cross_call(_local_event_unregister, event); 501ad6eb31eSJames Morse } 502ad6eb31eSJames Morse 503ad6eb31eSJames Morse int sdei_event_unregister(u32 event_num) 504ad6eb31eSJames Morse { 505ad6eb31eSJames Morse int err; 506ad6eb31eSJames Morse struct sdei_event *event; 507ad6eb31eSJames Morse 508ad6eb31eSJames Morse WARN_ON(in_nmi()); 509ad6eb31eSJames Morse 510ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 511ad6eb31eSJames Morse event = sdei_event_find(event_num); 512ad6eb31eSJames Morse do { 513ad6eb31eSJames Morse if (!event) { 514ad6eb31eSJames Morse pr_warn("Event %u not registered\n", event_num); 515ad6eb31eSJames Morse err = -ENOENT; 516ad6eb31eSJames Morse break; 517ad6eb31eSJames Morse } 518ad6eb31eSJames Morse 5196ded0b61SJames Morse spin_lock(&sdei_list_lock); 5206ded0b61SJames Morse event->reregister = false; 5216ded0b61SJames Morse event->reenable = false; 5226ded0b61SJames Morse spin_unlock(&sdei_list_lock); 5236ded0b61SJames Morse 524ad6eb31eSJames Morse err = _sdei_event_unregister(event); 525ad6eb31eSJames Morse if (err) 526ad6eb31eSJames Morse break; 527ad6eb31eSJames Morse 528ad6eb31eSJames Morse sdei_event_destroy(event); 529ad6eb31eSJames Morse } while (0); 530ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 531ad6eb31eSJames Morse 532ad6eb31eSJames Morse return err; 533ad6eb31eSJames Morse } 534ad6eb31eSJames Morse 535da351827SJames Morse /* 536da351827SJames Morse * unregister events, but don't destroy them as they are re-registered by 537da351827SJames Morse * sdei_reregister_shared(). 538da351827SJames Morse */ 539da351827SJames Morse static int sdei_unregister_shared(void) 540da351827SJames Morse { 541da351827SJames Morse int err = 0; 542da351827SJames Morse struct sdei_event *event; 543da351827SJames Morse 544da351827SJames Morse mutex_lock(&sdei_events_lock); 545da351827SJames Morse spin_lock(&sdei_list_lock); 546da351827SJames Morse list_for_each_entry(event, &sdei_list, list) { 547da351827SJames Morse if (event->type != SDEI_EVENT_TYPE_SHARED) 548da351827SJames Morse continue; 549da351827SJames Morse 550da351827SJames Morse err = _sdei_event_unregister(event); 551da351827SJames Morse if (err) 552da351827SJames Morse break; 553da351827SJames Morse } 554da351827SJames Morse spin_unlock(&sdei_list_lock); 555da351827SJames Morse mutex_unlock(&sdei_events_lock); 556da351827SJames Morse 557da351827SJames Morse return err; 558da351827SJames Morse } 559da351827SJames Morse 560ad6eb31eSJames Morse static int sdei_api_event_register(u32 event_num, unsigned long entry_point, 561ad6eb31eSJames Morse void *arg, u64 flags, u64 affinity) 562ad6eb31eSJames Morse { 563ad6eb31eSJames Morse return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_REGISTER, event_num, 564ad6eb31eSJames Morse (unsigned long)entry_point, (unsigned long)arg, 565ad6eb31eSJames Morse flags, affinity, NULL); 566ad6eb31eSJames Morse } 567ad6eb31eSJames Morse 568f92b5462SJames Morse /* Called directly by the hotplug callbacks */ 569f92b5462SJames Morse static void _local_event_register(void *data) 570f92b5462SJames Morse { 571f92b5462SJames Morse int err; 572f92b5462SJames Morse struct sdei_registered_event *reg; 573f92b5462SJames Morse struct sdei_crosscall_args *arg = data; 574f92b5462SJames Morse 575f92b5462SJames Morse WARN_ON(preemptible()); 576f92b5462SJames Morse 577f92b5462SJames Morse reg = per_cpu_ptr(arg->event->private_registered, smp_processor_id()); 578f92b5462SJames Morse err = sdei_api_event_register(arg->event->event_num, sdei_entry_point, 579f92b5462SJames Morse reg, 0, 0); 580f92b5462SJames Morse 581f92b5462SJames Morse sdei_cross_call_return(arg, err); 582f92b5462SJames Morse } 583f92b5462SJames Morse 584ad6eb31eSJames Morse static int _sdei_event_register(struct sdei_event *event) 585ad6eb31eSJames Morse { 586f92b5462SJames Morse int err; 587f92b5462SJames Morse 588ad6eb31eSJames Morse lockdep_assert_held(&sdei_events_lock); 589ad6eb31eSJames Morse 590ad6eb31eSJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 591ad6eb31eSJames Morse return sdei_api_event_register(event->event_num, 592ad6eb31eSJames Morse sdei_entry_point, 593ad6eb31eSJames Morse event->registered, 594ad6eb31eSJames Morse SDEI_EVENT_REGISTER_RM_ANY, 0); 595ad6eb31eSJames Morse 596f92b5462SJames Morse err = sdei_do_cross_call(_local_event_register, event); 5976ded0b61SJames Morse if (err) 598f92b5462SJames Morse sdei_do_cross_call(_local_event_unregister, event); 599f92b5462SJames Morse 600f92b5462SJames Morse return err; 601ad6eb31eSJames Morse } 602ad6eb31eSJames Morse 603ad6eb31eSJames Morse int sdei_event_register(u32 event_num, sdei_event_callback *cb, void *arg) 604ad6eb31eSJames Morse { 605ad6eb31eSJames Morse int err; 606ad6eb31eSJames Morse struct sdei_event *event; 607ad6eb31eSJames Morse 608ad6eb31eSJames Morse WARN_ON(in_nmi()); 609ad6eb31eSJames Morse 610ad6eb31eSJames Morse mutex_lock(&sdei_events_lock); 611ad6eb31eSJames Morse do { 612ad6eb31eSJames Morse if (sdei_event_find(event_num)) { 613ad6eb31eSJames Morse pr_warn("Event %u already registered\n", event_num); 614ad6eb31eSJames Morse err = -EBUSY; 615ad6eb31eSJames Morse break; 616ad6eb31eSJames Morse } 617ad6eb31eSJames Morse 618ad6eb31eSJames Morse event = sdei_event_create(event_num, cb, arg); 619ad6eb31eSJames Morse if (IS_ERR(event)) { 620ad6eb31eSJames Morse err = PTR_ERR(event); 621ad6eb31eSJames Morse pr_warn("Failed to create event %u: %d\n", event_num, 622ad6eb31eSJames Morse err); 623ad6eb31eSJames Morse break; 624ad6eb31eSJames Morse } 625ad6eb31eSJames Morse 62654f529a6SJames Morse cpus_read_lock(); 627ad6eb31eSJames Morse err = _sdei_event_register(event); 628ad6eb31eSJames Morse if (err) { 629ad6eb31eSJames Morse sdei_event_destroy(event); 630ad6eb31eSJames Morse pr_warn("Failed to register event %u: %d\n", event_num, 631ad6eb31eSJames Morse err); 63254f529a6SJames Morse } else { 63354f529a6SJames Morse spin_lock(&sdei_list_lock); 63454f529a6SJames Morse event->reregister = true; 63554f529a6SJames Morse spin_unlock(&sdei_list_lock); 636ad6eb31eSJames Morse } 63754f529a6SJames Morse cpus_read_unlock(); 638ad6eb31eSJames Morse } while (0); 639ad6eb31eSJames Morse mutex_unlock(&sdei_events_lock); 640ad6eb31eSJames Morse 641ad6eb31eSJames Morse return err; 642ad6eb31eSJames Morse } 643ad6eb31eSJames Morse 644c66d52b1SLiguang Zhang static int sdei_reregister_event_llocked(struct sdei_event *event) 645da351827SJames Morse { 646da351827SJames Morse int err; 647da351827SJames Morse 648da351827SJames Morse lockdep_assert_held(&sdei_events_lock); 649c66d52b1SLiguang Zhang lockdep_assert_held(&sdei_list_lock); 650da351827SJames Morse 651da351827SJames Morse err = _sdei_event_register(event); 652da351827SJames Morse if (err) { 653da351827SJames Morse pr_err("Failed to re-register event %u\n", event->event_num); 654c66d52b1SLiguang Zhang sdei_event_destroy_llocked(event); 655da351827SJames Morse return err; 656da351827SJames Morse } 657da351827SJames Morse 658da351827SJames Morse if (event->reenable) { 659da351827SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 660da351827SJames Morse err = sdei_api_event_enable(event->event_num); 661f92b5462SJames Morse else 662f92b5462SJames Morse err = sdei_do_cross_call(_local_event_enable, event); 663da351827SJames Morse } 664da351827SJames Morse 665da351827SJames Morse if (err) 666da351827SJames Morse pr_err("Failed to re-enable event %u\n", event->event_num); 667da351827SJames Morse 668da351827SJames Morse return err; 669da351827SJames Morse } 670da351827SJames Morse 671da351827SJames Morse static int sdei_reregister_shared(void) 672da351827SJames Morse { 673da351827SJames Morse int err = 0; 674da351827SJames Morse struct sdei_event *event; 675da351827SJames Morse 676da351827SJames Morse mutex_lock(&sdei_events_lock); 677da351827SJames Morse spin_lock(&sdei_list_lock); 678da351827SJames Morse list_for_each_entry(event, &sdei_list, list) { 679da351827SJames Morse if (event->type != SDEI_EVENT_TYPE_SHARED) 680da351827SJames Morse continue; 681da351827SJames Morse 682da351827SJames Morse if (event->reregister) { 683c66d52b1SLiguang Zhang err = sdei_reregister_event_llocked(event); 684da351827SJames Morse if (err) 685da351827SJames Morse break; 686da351827SJames Morse } 687da351827SJames Morse } 688da351827SJames Morse spin_unlock(&sdei_list_lock); 689da351827SJames Morse mutex_unlock(&sdei_events_lock); 690da351827SJames Morse 691da351827SJames Morse return err; 692da351827SJames Morse } 693da351827SJames Morse 694f92b5462SJames Morse static int sdei_cpuhp_down(unsigned int cpu) 695f92b5462SJames Morse { 696f92b5462SJames Morse struct sdei_event *event; 697f92b5462SJames Morse struct sdei_crosscall_args arg; 698f92b5462SJames Morse 699f92b5462SJames Morse /* un-register private events */ 700f92b5462SJames Morse spin_lock(&sdei_list_lock); 701f92b5462SJames Morse list_for_each_entry(event, &sdei_list, list) { 702f92b5462SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 703f92b5462SJames Morse continue; 704f92b5462SJames Morse 705f92b5462SJames Morse CROSSCALL_INIT(arg, event); 706f92b5462SJames Morse /* call the cross-call function locally... */ 707f92b5462SJames Morse _local_event_unregister(&arg); 708f92b5462SJames Morse if (arg.first_error) 709f92b5462SJames Morse pr_err("Failed to unregister event %u: %d\n", 710f92b5462SJames Morse event->event_num, arg.first_error); 711f92b5462SJames Morse } 712f92b5462SJames Morse spin_unlock(&sdei_list_lock); 713f92b5462SJames Morse 714f92b5462SJames Morse return sdei_mask_local_cpu(); 715f92b5462SJames Morse } 716f92b5462SJames Morse 717f92b5462SJames Morse static int sdei_cpuhp_up(unsigned int cpu) 718f92b5462SJames Morse { 719f92b5462SJames Morse struct sdei_event *event; 720f92b5462SJames Morse struct sdei_crosscall_args arg; 721f92b5462SJames Morse 722f92b5462SJames Morse /* re-register/enable private events */ 723f92b5462SJames Morse spin_lock(&sdei_list_lock); 724f92b5462SJames Morse list_for_each_entry(event, &sdei_list, list) { 725f92b5462SJames Morse if (event->type == SDEI_EVENT_TYPE_SHARED) 726f92b5462SJames Morse continue; 727f92b5462SJames Morse 728f92b5462SJames Morse if (event->reregister) { 729f92b5462SJames Morse CROSSCALL_INIT(arg, event); 730f92b5462SJames Morse /* call the cross-call function locally... */ 731f92b5462SJames Morse _local_event_register(&arg); 732f92b5462SJames Morse if (arg.first_error) 733f92b5462SJames Morse pr_err("Failed to re-register event %u: %d\n", 734f92b5462SJames Morse event->event_num, arg.first_error); 735f92b5462SJames Morse } 736f92b5462SJames Morse 737f92b5462SJames Morse if (event->reenable) { 738f92b5462SJames Morse CROSSCALL_INIT(arg, event); 739f92b5462SJames Morse _local_event_enable(&arg); 740f92b5462SJames Morse if (arg.first_error) 741f92b5462SJames Morse pr_err("Failed to re-enable event %u: %d\n", 742f92b5462SJames Morse event->event_num, arg.first_error); 743f92b5462SJames Morse } 744f92b5462SJames Morse } 745f92b5462SJames Morse spin_unlock(&sdei_list_lock); 746f92b5462SJames Morse 747f92b5462SJames Morse return sdei_unmask_local_cpu(); 748f92b5462SJames Morse } 749f92b5462SJames Morse 750da351827SJames Morse /* When entering idle, mask/unmask events for this cpu */ 751da351827SJames Morse static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action, 752da351827SJames Morse void *data) 753da351827SJames Morse { 754da351827SJames Morse int rv; 755da351827SJames Morse 756da351827SJames Morse switch (action) { 757da351827SJames Morse case CPU_PM_ENTER: 758da351827SJames Morse rv = sdei_mask_local_cpu(); 759da351827SJames Morse break; 760da351827SJames Morse case CPU_PM_EXIT: 761da351827SJames Morse case CPU_PM_ENTER_FAILED: 762da351827SJames Morse rv = sdei_unmask_local_cpu(); 763da351827SJames Morse break; 764da351827SJames Morse default: 765da351827SJames Morse return NOTIFY_DONE; 766da351827SJames Morse } 767da351827SJames Morse 768da351827SJames Morse if (rv) 769da351827SJames Morse return notifier_from_errno(rv); 770da351827SJames Morse 771da351827SJames Morse return NOTIFY_OK; 772da351827SJames Morse } 773da351827SJames Morse 774da351827SJames Morse static struct notifier_block sdei_pm_nb = { 775da351827SJames Morse .notifier_call = sdei_pm_notifier, 776da351827SJames Morse }; 777da351827SJames Morse 778da351827SJames Morse static int sdei_device_suspend(struct device *dev) 779da351827SJames Morse { 780da351827SJames Morse on_each_cpu(_ipi_mask_cpu, NULL, true); 781da351827SJames Morse 782da351827SJames Morse return 0; 783da351827SJames Morse } 784da351827SJames Morse 785da351827SJames Morse static int sdei_device_resume(struct device *dev) 786da351827SJames Morse { 787da351827SJames Morse on_each_cpu(_ipi_unmask_cpu, NULL, true); 788da351827SJames Morse 789da351827SJames Morse return 0; 790da351827SJames Morse } 791da351827SJames Morse 792da351827SJames Morse /* 793da351827SJames Morse * We need all events to be reregistered when we resume from hibernate. 794da351827SJames Morse * 795da351827SJames Morse * The sequence is freeze->thaw. Reboot. freeze->restore. We unregister 796da351827SJames Morse * events during freeze, then re-register and re-enable them during thaw 797da351827SJames Morse * and restore. 798da351827SJames Morse */ 799da351827SJames Morse static int sdei_device_freeze(struct device *dev) 800da351827SJames Morse { 801da351827SJames Morse int err; 802da351827SJames Morse 803f92b5462SJames Morse /* unregister private events */ 804da351827SJames Morse cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); 805da351827SJames Morse 806da351827SJames Morse err = sdei_unregister_shared(); 807da351827SJames Morse if (err) 808da351827SJames Morse return err; 809da351827SJames Morse 810da351827SJames Morse return 0; 811da351827SJames Morse } 812da351827SJames Morse 813da351827SJames Morse static int sdei_device_thaw(struct device *dev) 814da351827SJames Morse { 815da351827SJames Morse int err; 816da351827SJames Morse 817da351827SJames Morse /* re-register shared events */ 818da351827SJames Morse err = sdei_reregister_shared(); 819da351827SJames Morse if (err) { 820da351827SJames Morse pr_warn("Failed to re-register shared events...\n"); 821da351827SJames Morse sdei_mark_interface_broken(); 822da351827SJames Morse return err; 823da351827SJames Morse } 824da351827SJames Morse 825da351827SJames Morse err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", 826da351827SJames Morse &sdei_cpuhp_up, &sdei_cpuhp_down); 827da351827SJames Morse if (err) 828da351827SJames Morse pr_warn("Failed to re-register CPU hotplug notifier...\n"); 829da351827SJames Morse 830da351827SJames Morse return err; 831da351827SJames Morse } 832da351827SJames Morse 833da351827SJames Morse static int sdei_device_restore(struct device *dev) 834da351827SJames Morse { 835da351827SJames Morse int err; 836da351827SJames Morse 837da351827SJames Morse err = sdei_platform_reset(); 838da351827SJames Morse if (err) 839da351827SJames Morse return err; 840da351827SJames Morse 841da351827SJames Morse return sdei_device_thaw(dev); 842da351827SJames Morse } 843da351827SJames Morse 844da351827SJames Morse static const struct dev_pm_ops sdei_pm_ops = { 845da351827SJames Morse .suspend = sdei_device_suspend, 846da351827SJames Morse .resume = sdei_device_resume, 847da351827SJames Morse .freeze = sdei_device_freeze, 848da351827SJames Morse .thaw = sdei_device_thaw, 849da351827SJames Morse .restore = sdei_device_restore, 850da351827SJames Morse }; 851da351827SJames Morse 852da351827SJames Morse /* 853da351827SJames Morse * Mask all CPUs and unregister all events on panic, reboot or kexec. 854da351827SJames Morse */ 855da351827SJames Morse static int sdei_reboot_notifier(struct notifier_block *nb, unsigned long action, 856da351827SJames Morse void *data) 857da351827SJames Morse { 858da351827SJames Morse /* 859da351827SJames Morse * We are going to reset the interface, after this there is no point 860da351827SJames Morse * doing work when we take CPUs offline. 861da351827SJames Morse */ 862da351827SJames Morse cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); 863da351827SJames Morse 864da351827SJames Morse sdei_platform_reset(); 865da351827SJames Morse 866da351827SJames Morse return NOTIFY_OK; 867da351827SJames Morse } 868da351827SJames Morse 869da351827SJames Morse static struct notifier_block sdei_reboot_nb = { 870da351827SJames Morse .notifier_call = sdei_reboot_notifier, 871da351827SJames Morse }; 872da351827SJames Morse 873ad6eb31eSJames Morse static void sdei_smccc_smc(unsigned long function_id, 874ad6eb31eSJames Morse unsigned long arg0, unsigned long arg1, 875ad6eb31eSJames Morse unsigned long arg2, unsigned long arg3, 876ad6eb31eSJames Morse unsigned long arg4, struct arm_smccc_res *res) 877ad6eb31eSJames Morse { 878ad6eb31eSJames Morse arm_smccc_smc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); 879ad6eb31eSJames Morse } 8802f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(sdei_smccc_smc); 881ad6eb31eSJames Morse 882ad6eb31eSJames Morse static void sdei_smccc_hvc(unsigned long function_id, 883ad6eb31eSJames Morse unsigned long arg0, unsigned long arg1, 884ad6eb31eSJames Morse unsigned long arg2, unsigned long arg3, 885ad6eb31eSJames Morse unsigned long arg4, struct arm_smccc_res *res) 886ad6eb31eSJames Morse { 887ad6eb31eSJames Morse arm_smccc_hvc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); 888ad6eb31eSJames Morse } 8892f1d4e24SXiongfeng Wang NOKPROBE_SYMBOL(sdei_smccc_hvc); 890ad6eb31eSJames Morse 891f96935d3SJames Morse int sdei_register_ghes(struct ghes *ghes, sdei_event_callback *normal_cb, 892f96935d3SJames Morse sdei_event_callback *critical_cb) 893f96935d3SJames Morse { 894f96935d3SJames Morse int err; 895f96935d3SJames Morse u64 result; 896f96935d3SJames Morse u32 event_num; 897f96935d3SJames Morse sdei_event_callback *cb; 898f96935d3SJames Morse 899f96935d3SJames Morse if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES)) 900f96935d3SJames Morse return -EOPNOTSUPP; 901f96935d3SJames Morse 902f96935d3SJames Morse event_num = ghes->generic->notify.vector; 903f96935d3SJames Morse if (event_num == 0) { 904f96935d3SJames Morse /* 905f96935d3SJames Morse * Event 0 is reserved by the specification for 906f96935d3SJames Morse * SDEI_EVENT_SIGNAL. 907f96935d3SJames Morse */ 908f96935d3SJames Morse return -EINVAL; 909f96935d3SJames Morse } 910f96935d3SJames Morse 911f96935d3SJames Morse err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY, 912f96935d3SJames Morse &result); 913f96935d3SJames Morse if (err) 914f96935d3SJames Morse return err; 915f96935d3SJames Morse 916f96935d3SJames Morse if (result == SDEI_EVENT_PRIORITY_CRITICAL) 917f96935d3SJames Morse cb = critical_cb; 918f96935d3SJames Morse else 919f96935d3SJames Morse cb = normal_cb; 920f96935d3SJames Morse 921f96935d3SJames Morse err = sdei_event_register(event_num, cb, ghes); 922f96935d3SJames Morse if (!err) 923f96935d3SJames Morse err = sdei_event_enable(event_num); 924f96935d3SJames Morse 925f96935d3SJames Morse return err; 926f96935d3SJames Morse } 927f96935d3SJames Morse 928f96935d3SJames Morse int sdei_unregister_ghes(struct ghes *ghes) 929f96935d3SJames Morse { 930f96935d3SJames Morse int i; 931f96935d3SJames Morse int err; 932f96935d3SJames Morse u32 event_num = ghes->generic->notify.vector; 933f96935d3SJames Morse 934f96935d3SJames Morse might_sleep(); 935f96935d3SJames Morse 936f96935d3SJames Morse if (!IS_ENABLED(CONFIG_ACPI_APEI_GHES)) 937f96935d3SJames Morse return -EOPNOTSUPP; 938f96935d3SJames Morse 939f96935d3SJames Morse /* 940f96935d3SJames Morse * The event may be running on another CPU. Disable it 941f96935d3SJames Morse * to stop new events, then try to unregister a few times. 942f96935d3SJames Morse */ 943f96935d3SJames Morse err = sdei_event_disable(event_num); 944f96935d3SJames Morse if (err) 945f96935d3SJames Morse return err; 946f96935d3SJames Morse 947f96935d3SJames Morse for (i = 0; i < 3; i++) { 948f96935d3SJames Morse err = sdei_event_unregister(event_num); 949f96935d3SJames Morse if (err != -EINPROGRESS) 950f96935d3SJames Morse break; 951f96935d3SJames Morse 952f96935d3SJames Morse schedule(); 953f96935d3SJames Morse } 954f96935d3SJames Morse 955f96935d3SJames Morse return err; 956f96935d3SJames Morse } 957f96935d3SJames Morse 958ad6eb31eSJames Morse static int sdei_get_conduit(struct platform_device *pdev) 959ad6eb31eSJames Morse { 960ad6eb31eSJames Morse const char *method; 961ad6eb31eSJames Morse struct device_node *np = pdev->dev.of_node; 962ad6eb31eSJames Morse 963ad6eb31eSJames Morse sdei_firmware_call = NULL; 964ad6eb31eSJames Morse if (np) { 965ad6eb31eSJames Morse if (of_property_read_string(np, "method", &method)) { 966ad6eb31eSJames Morse pr_warn("missing \"method\" property\n"); 967e6ea4651SMark Rutland return SMCCC_CONDUIT_NONE; 968ad6eb31eSJames Morse } 969ad6eb31eSJames Morse 970ad6eb31eSJames Morse if (!strcmp("hvc", method)) { 971ad6eb31eSJames Morse sdei_firmware_call = &sdei_smccc_hvc; 972e6ea4651SMark Rutland return SMCCC_CONDUIT_HVC; 973ad6eb31eSJames Morse } else if (!strcmp("smc", method)) { 974ad6eb31eSJames Morse sdei_firmware_call = &sdei_smccc_smc; 975e6ea4651SMark Rutland return SMCCC_CONDUIT_SMC; 976ad6eb31eSJames Morse } 977ad6eb31eSJames Morse 978ad6eb31eSJames Morse pr_warn("invalid \"method\" property: %s\n", method); 979677a60bdSJames Morse } else if (IS_ENABLED(CONFIG_ACPI) && !acpi_disabled) { 980677a60bdSJames Morse if (acpi_psci_use_hvc()) { 981677a60bdSJames Morse sdei_firmware_call = &sdei_smccc_hvc; 982e6ea4651SMark Rutland return SMCCC_CONDUIT_HVC; 983677a60bdSJames Morse } else { 984677a60bdSJames Morse sdei_firmware_call = &sdei_smccc_smc; 985e6ea4651SMark Rutland return SMCCC_CONDUIT_SMC; 986677a60bdSJames Morse } 987ad6eb31eSJames Morse } 988ad6eb31eSJames Morse 989e6ea4651SMark Rutland return SMCCC_CONDUIT_NONE; 990ad6eb31eSJames Morse } 991ad6eb31eSJames Morse 992ad6eb31eSJames Morse static int sdei_probe(struct platform_device *pdev) 993ad6eb31eSJames Morse { 994ad6eb31eSJames Morse int err; 995ad6eb31eSJames Morse u64 ver = 0; 996ad6eb31eSJames Morse int conduit; 997ad6eb31eSJames Morse 998ad6eb31eSJames Morse conduit = sdei_get_conduit(pdev); 999ad6eb31eSJames Morse if (!sdei_firmware_call) 1000ad6eb31eSJames Morse return 0; 1001ad6eb31eSJames Morse 1002ad6eb31eSJames Morse err = sdei_api_get_version(&ver); 1003ad6eb31eSJames Morse if (err == -EOPNOTSUPP) 1004ad6eb31eSJames Morse pr_err("advertised but not implemented in platform firmware\n"); 1005ad6eb31eSJames Morse if (err) { 1006ad6eb31eSJames Morse pr_err("Failed to get SDEI version: %d\n", err); 1007ad6eb31eSJames Morse sdei_mark_interface_broken(); 1008ad6eb31eSJames Morse return err; 1009ad6eb31eSJames Morse } 1010ad6eb31eSJames Morse 1011ad6eb31eSJames Morse pr_info("SDEIv%d.%d (0x%x) detected in firmware.\n", 1012ad6eb31eSJames Morse (int)SDEI_VERSION_MAJOR(ver), (int)SDEI_VERSION_MINOR(ver), 1013ad6eb31eSJames Morse (int)SDEI_VERSION_VENDOR(ver)); 1014ad6eb31eSJames Morse 1015ad6eb31eSJames Morse if (SDEI_VERSION_MAJOR(ver) != 1) { 1016ad6eb31eSJames Morse pr_warn("Conflicting SDEI version detected.\n"); 1017ad6eb31eSJames Morse sdei_mark_interface_broken(); 1018ad6eb31eSJames Morse return -EINVAL; 1019ad6eb31eSJames Morse } 1020ad6eb31eSJames Morse 1021ad6eb31eSJames Morse err = sdei_platform_reset(); 1022ad6eb31eSJames Morse if (err) 1023ad6eb31eSJames Morse return err; 1024ad6eb31eSJames Morse 1025ad6eb31eSJames Morse sdei_entry_point = sdei_arch_get_entry_point(conduit); 1026ad6eb31eSJames Morse if (!sdei_entry_point) { 1027ad6eb31eSJames Morse /* Not supported due to hardware or boot configuration */ 1028ad6eb31eSJames Morse sdei_mark_interface_broken(); 1029ad6eb31eSJames Morse return 0; 1030ad6eb31eSJames Morse } 1031ad6eb31eSJames Morse 1032da351827SJames Morse err = cpu_pm_register_notifier(&sdei_pm_nb); 1033da351827SJames Morse if (err) { 1034da351827SJames Morse pr_warn("Failed to register CPU PM notifier...\n"); 1035da351827SJames Morse goto error; 1036da351827SJames Morse } 1037da351827SJames Morse 1038da351827SJames Morse err = register_reboot_notifier(&sdei_reboot_nb); 1039da351827SJames Morse if (err) { 1040da351827SJames Morse pr_warn("Failed to register reboot notifier...\n"); 1041da351827SJames Morse goto remove_cpupm; 1042da351827SJames Morse } 1043da351827SJames Morse 1044da351827SJames Morse err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", 1045da351827SJames Morse &sdei_cpuhp_up, &sdei_cpuhp_down); 1046da351827SJames Morse if (err) { 1047da351827SJames Morse pr_warn("Failed to register CPU hotplug notifier...\n"); 1048da351827SJames Morse goto remove_reboot; 1049da351827SJames Morse } 1050ad6eb31eSJames Morse 1051ad6eb31eSJames Morse return 0; 1052da351827SJames Morse 1053da351827SJames Morse remove_reboot: 1054da351827SJames Morse unregister_reboot_notifier(&sdei_reboot_nb); 1055da351827SJames Morse 1056da351827SJames Morse remove_cpupm: 1057da351827SJames Morse cpu_pm_unregister_notifier(&sdei_pm_nb); 1058da351827SJames Morse 1059da351827SJames Morse error: 1060da351827SJames Morse sdei_mark_interface_broken(); 1061da351827SJames Morse return err; 1062ad6eb31eSJames Morse } 1063ad6eb31eSJames Morse 1064ad6eb31eSJames Morse static const struct of_device_id sdei_of_match[] = { 1065ad6eb31eSJames Morse { .compatible = "arm,sdei-1.0" }, 1066ad6eb31eSJames Morse {} 1067ad6eb31eSJames Morse }; 1068ad6eb31eSJames Morse 1069ad6eb31eSJames Morse static struct platform_driver sdei_driver = { 1070ad6eb31eSJames Morse .driver = { 1071ad6eb31eSJames Morse .name = "sdei", 1072da351827SJames Morse .pm = &sdei_pm_ops, 1073ad6eb31eSJames Morse .of_match_table = sdei_of_match, 1074ad6eb31eSJames Morse }, 1075ad6eb31eSJames Morse .probe = sdei_probe, 1076ad6eb31eSJames Morse }; 1077ad6eb31eSJames Morse 1078677a60bdSJames Morse static bool __init sdei_present_acpi(void) 1079677a60bdSJames Morse { 1080677a60bdSJames Morse acpi_status status; 1081677a60bdSJames Morse struct acpi_table_header *sdei_table_header; 1082677a60bdSJames Morse 1083677a60bdSJames Morse if (acpi_disabled) 1084677a60bdSJames Morse return false; 1085677a60bdSJames Morse 1086677a60bdSJames Morse status = acpi_get_table(ACPI_SIG_SDEI, 0, &sdei_table_header); 1087677a60bdSJames Morse if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { 1088677a60bdSJames Morse const char *msg = acpi_format_exception(status); 1089677a60bdSJames Morse 1090677a60bdSJames Morse pr_info("Failed to get ACPI:SDEI table, %s\n", msg); 1091677a60bdSJames Morse } 1092677a60bdSJames Morse if (ACPI_FAILURE(status)) 1093677a60bdSJames Morse return false; 1094677a60bdSJames Morse 109570e6352aSHanjun Guo acpi_put_table(sdei_table_header); 109670e6352aSHanjun Guo 1097677a60bdSJames Morse return true; 1098677a60bdSJames Morse } 1099677a60bdSJames Morse 1100ad6eb31eSJames Morse static int __init sdei_init(void) 1101ad6eb31eSJames Morse { 1102caf2cd61SSudeep Holla int ret = platform_driver_register(&sdei_driver); 1103ad6eb31eSJames Morse 1104caf2cd61SSudeep Holla if (!ret && sdei_present_acpi()) { 1105caf2cd61SSudeep Holla struct platform_device *pdev; 1106caf2cd61SSudeep Holla 1107caf2cd61SSudeep Holla pdev = platform_device_register_simple(sdei_driver.driver.name, 1108caf2cd61SSudeep Holla 0, NULL, 0); 1109caf2cd61SSudeep Holla if (IS_ERR(pdev)) 1110caf2cd61SSudeep Holla pr_info("Failed to register ACPI:SDEI platform device %ld\n", 1111caf2cd61SSudeep Holla PTR_ERR(pdev)); 1112caf2cd61SSudeep Holla } 1113caf2cd61SSudeep Holla 1114caf2cd61SSudeep Holla return ret; 1115ad6eb31eSJames Morse } 1116ad6eb31eSJames Morse 1117677a60bdSJames Morse /* 1118677a60bdSJames Morse * On an ACPI system SDEI needs to be ready before HEST:GHES tries to register 1119677a60bdSJames Morse * its events. ACPI is initialised from a subsys_initcall(), GHES is initialised 1120677a60bdSJames Morse * by device_initcall(). We want to be called in the middle. 1121677a60bdSJames Morse */ 1122ad6eb31eSJames Morse subsys_initcall_sync(sdei_init); 1123ad6eb31eSJames Morse 1124ad6eb31eSJames Morse int sdei_event_handler(struct pt_regs *regs, 1125ad6eb31eSJames Morse struct sdei_registered_event *arg) 1126ad6eb31eSJames Morse { 1127ad6eb31eSJames Morse int err; 1128ad6eb31eSJames Morse mm_segment_t orig_addr_limit; 1129ad6eb31eSJames Morse u32 event_num = arg->event_num; 1130ad6eb31eSJames Morse 1131472de63bSJames Morse /* 1132472de63bSJames Morse * Save restore 'fs'. 1133472de63bSJames Morse * The architecture's entry code save/restores 'fs' when taking an 1134472de63bSJames Morse * exception from the kernel. This ensures addr_limit isn't inherited 1135472de63bSJames Morse * if you interrupted something that allowed the uaccess routines to 1136472de63bSJames Morse * access kernel memory. 1137472de63bSJames Morse * Do the same here because this doesn't come via the same entry code. 1138472de63bSJames Morse */ 11393d13f313SChristoph Hellwig orig_addr_limit = force_uaccess_begin(); 1140ad6eb31eSJames Morse 1141ad6eb31eSJames Morse err = arg->callback(event_num, regs, arg->callback_arg); 1142ad6eb31eSJames Morse if (err) 1143ad6eb31eSJames Morse pr_err_ratelimited("event %u on CPU %u failed with error: %d\n", 1144ad6eb31eSJames Morse event_num, smp_processor_id(), err); 1145ad6eb31eSJames Morse 11463d13f313SChristoph Hellwig force_uaccess_end(orig_addr_limit); 1147ad6eb31eSJames Morse 1148ad6eb31eSJames Morse return err; 1149ad6eb31eSJames Morse } 1150ad6eb31eSJames Morse NOKPROBE_SYMBOL(sdei_event_handler); 1151