James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (C) 2017 Arm Ltd. |
| 3 | #define pr_fmt(fmt) "sdei: " fmt |
| 4 | |
| 5 | #include <linux/acpi.h> |
| 6 | #include <linux/arm_sdei.h> |
| 7 | #include <linux/arm-smccc.h> |
| 8 | #include <linux/bitops.h> |
| 9 | #include <linux/compiler.h> |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 10 | #include <linux/cpuhotplug.h> |
| 11 | #include <linux/cpu_pm.h> |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 12 | #include <linux/errno.h> |
| 13 | #include <linux/hardirq.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/kprobes.h> |
| 16 | #include <linux/kvm_host.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/mutex.h> |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 19 | #include <linux/notifier.h> |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/percpu.h> |
| 23 | #include <linux/platform_device.h> |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 24 | #include <linux/pm.h> |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 25 | #include <linux/ptrace.h> |
| 26 | #include <linux/preempt.h> |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 27 | #include <linux/reboot.h> |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 28 | #include <linux/slab.h> |
| 29 | #include <linux/smp.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | |
| 33 | /* |
| 34 | * The call to use to reach the firmware. |
| 35 | */ |
| 36 | static asmlinkage void (*sdei_firmware_call)(unsigned long function_id, |
| 37 | unsigned long arg0, unsigned long arg1, |
| 38 | unsigned long arg2, unsigned long arg3, |
| 39 | unsigned long arg4, struct arm_smccc_res *res); |
| 40 | |
| 41 | /* entry point from firmware to arch asm code */ |
| 42 | static unsigned long sdei_entry_point; |
| 43 | |
| 44 | struct sdei_event { |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 45 | /* These three are protected by the sdei_list_lock */ |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 46 | struct list_head list; |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 47 | bool reregister; |
| 48 | bool reenable; |
| 49 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 50 | u32 event_num; |
| 51 | u8 type; |
| 52 | u8 priority; |
| 53 | |
| 54 | /* This pointer is handed to firmware as the event argument. */ |
| 55 | struct sdei_registered_event *registered; |
| 56 | }; |
| 57 | |
| 58 | /* Take the mutex for any API call or modification. Take the mutex first. */ |
| 59 | static DEFINE_MUTEX(sdei_events_lock); |
| 60 | |
| 61 | /* and then hold this when modifying the list */ |
| 62 | static DEFINE_SPINLOCK(sdei_list_lock); |
| 63 | static LIST_HEAD(sdei_list); |
| 64 | |
| 65 | static int sdei_to_linux_errno(unsigned long sdei_err) |
| 66 | { |
| 67 | switch (sdei_err) { |
| 68 | case SDEI_NOT_SUPPORTED: |
| 69 | return -EOPNOTSUPP; |
| 70 | case SDEI_INVALID_PARAMETERS: |
| 71 | return -EINVAL; |
| 72 | case SDEI_DENIED: |
| 73 | return -EPERM; |
| 74 | case SDEI_PENDING: |
| 75 | return -EINPROGRESS; |
| 76 | case SDEI_OUT_OF_RESOURCE: |
| 77 | return -ENOMEM; |
| 78 | } |
| 79 | |
| 80 | /* Not an error value ... */ |
| 81 | return sdei_err; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * If x0 is any of these values, then the call failed, use sdei_to_linux_errno() |
| 86 | * to translate. |
| 87 | */ |
| 88 | static int sdei_is_err(struct arm_smccc_res *res) |
| 89 | { |
| 90 | switch (res->a0) { |
| 91 | case SDEI_NOT_SUPPORTED: |
| 92 | case SDEI_INVALID_PARAMETERS: |
| 93 | case SDEI_DENIED: |
| 94 | case SDEI_PENDING: |
| 95 | case SDEI_OUT_OF_RESOURCE: |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | static int invoke_sdei_fn(unsigned long function_id, unsigned long arg0, |
| 103 | unsigned long arg1, unsigned long arg2, |
| 104 | unsigned long arg3, unsigned long arg4, |
| 105 | u64 *result) |
| 106 | { |
| 107 | int err = 0; |
| 108 | struct arm_smccc_res res; |
| 109 | |
| 110 | if (sdei_firmware_call) { |
| 111 | sdei_firmware_call(function_id, arg0, arg1, arg2, arg3, arg4, |
| 112 | &res); |
| 113 | if (sdei_is_err(&res)) |
| 114 | err = sdei_to_linux_errno(res.a0); |
| 115 | } else { |
| 116 | /* |
| 117 | * !sdei_firmware_call means we failed to probe or called |
| 118 | * sdei_mark_interface_broken(). -EIO is not an error returned |
| 119 | * by sdei_to_linux_errno() and is used to suppress messages |
| 120 | * from this driver. |
| 121 | */ |
| 122 | err = -EIO; |
| 123 | res.a0 = SDEI_NOT_SUPPORTED; |
| 124 | } |
| 125 | |
| 126 | if (result) |
| 127 | *result = res.a0; |
| 128 | |
| 129 | return err; |
| 130 | } |
| 131 | |
| 132 | static struct sdei_event *sdei_event_find(u32 event_num) |
| 133 | { |
| 134 | struct sdei_event *e, *found = NULL; |
| 135 | |
| 136 | lockdep_assert_held(&sdei_events_lock); |
| 137 | |
| 138 | spin_lock(&sdei_list_lock); |
| 139 | list_for_each_entry(e, &sdei_list, list) { |
| 140 | if (e->event_num == event_num) { |
| 141 | found = e; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | spin_unlock(&sdei_list_lock); |
| 146 | |
| 147 | return found; |
| 148 | } |
| 149 | |
| 150 | int sdei_api_event_context(u32 query, u64 *result) |
| 151 | { |
| 152 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_CONTEXT, query, 0, 0, 0, 0, |
| 153 | result); |
| 154 | } |
| 155 | NOKPROBE_SYMBOL(sdei_api_event_context); |
| 156 | |
| 157 | static int sdei_api_event_get_info(u32 event, u32 info, u64 *result) |
| 158 | { |
| 159 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_GET_INFO, event, info, 0, |
| 160 | 0, 0, result); |
| 161 | } |
| 162 | |
| 163 | static struct sdei_event *sdei_event_create(u32 event_num, |
| 164 | sdei_event_callback *cb, |
| 165 | void *cb_arg) |
| 166 | { |
| 167 | int err; |
| 168 | u64 result; |
| 169 | struct sdei_event *event; |
| 170 | struct sdei_registered_event *reg; |
| 171 | |
| 172 | lockdep_assert_held(&sdei_events_lock); |
| 173 | |
| 174 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 175 | if (!event) |
| 176 | return ERR_PTR(-ENOMEM); |
| 177 | |
| 178 | INIT_LIST_HEAD(&event->list); |
| 179 | event->event_num = event_num; |
| 180 | |
| 181 | err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_PRIORITY, |
| 182 | &result); |
| 183 | if (err) { |
| 184 | kfree(event); |
| 185 | return ERR_PTR(err); |
| 186 | } |
| 187 | event->priority = result; |
| 188 | |
| 189 | err = sdei_api_event_get_info(event_num, SDEI_EVENT_INFO_EV_TYPE, |
| 190 | &result); |
| 191 | if (err) { |
| 192 | kfree(event); |
| 193 | return ERR_PTR(err); |
| 194 | } |
| 195 | event->type = result; |
| 196 | |
| 197 | if (event->type == SDEI_EVENT_TYPE_SHARED) { |
| 198 | reg = kzalloc(sizeof(*reg), GFP_KERNEL); |
| 199 | if (!reg) { |
| 200 | kfree(event); |
| 201 | return ERR_PTR(-ENOMEM); |
| 202 | } |
| 203 | |
| 204 | reg->event_num = event_num; |
| 205 | reg->priority = event->priority; |
| 206 | |
| 207 | reg->callback = cb; |
| 208 | reg->callback_arg = cb_arg; |
| 209 | event->registered = reg; |
| 210 | } |
| 211 | |
| 212 | if (sdei_event_find(event_num)) { |
| 213 | kfree(event->registered); |
| 214 | kfree(event); |
| 215 | event = ERR_PTR(-EBUSY); |
| 216 | } else { |
| 217 | spin_lock(&sdei_list_lock); |
| 218 | list_add(&event->list, &sdei_list); |
| 219 | spin_unlock(&sdei_list_lock); |
| 220 | } |
| 221 | |
| 222 | return event; |
| 223 | } |
| 224 | |
| 225 | static void sdei_event_destroy(struct sdei_event *event) |
| 226 | { |
| 227 | lockdep_assert_held(&sdei_events_lock); |
| 228 | |
| 229 | spin_lock(&sdei_list_lock); |
| 230 | list_del(&event->list); |
| 231 | spin_unlock(&sdei_list_lock); |
| 232 | |
| 233 | if (event->type == SDEI_EVENT_TYPE_SHARED) |
| 234 | kfree(event->registered); |
| 235 | |
| 236 | kfree(event); |
| 237 | } |
| 238 | |
| 239 | static int sdei_api_get_version(u64 *version) |
| 240 | { |
| 241 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_VERSION, 0, 0, 0, 0, 0, version); |
| 242 | } |
| 243 | |
| 244 | int sdei_mask_local_cpu(void) |
| 245 | { |
| 246 | int err; |
| 247 | |
| 248 | WARN_ON_ONCE(preemptible()); |
| 249 | |
| 250 | err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_MASK, 0, 0, 0, 0, 0, NULL); |
| 251 | if (err && err != -EIO) { |
| 252 | pr_warn_once("failed to mask CPU[%u]: %d\n", |
| 253 | smp_processor_id(), err); |
| 254 | return err; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void _ipi_mask_cpu(void *ignored) |
| 261 | { |
| 262 | sdei_mask_local_cpu(); |
| 263 | } |
| 264 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 265 | static int sdei_cpuhp_down(unsigned int ignored) |
| 266 | { |
| 267 | return sdei_mask_local_cpu(); |
| 268 | } |
| 269 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 270 | int sdei_unmask_local_cpu(void) |
| 271 | { |
| 272 | int err; |
| 273 | |
| 274 | WARN_ON_ONCE(preemptible()); |
| 275 | |
| 276 | err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PE_UNMASK, 0, 0, 0, 0, 0, NULL); |
| 277 | if (err && err != -EIO) { |
| 278 | pr_warn_once("failed to unmask CPU[%u]: %d\n", |
| 279 | smp_processor_id(), err); |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static void _ipi_unmask_cpu(void *ignored) |
| 287 | { |
| 288 | sdei_unmask_local_cpu(); |
| 289 | } |
| 290 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 291 | static int sdei_cpuhp_up(unsigned int ignored) |
| 292 | { |
| 293 | return sdei_unmask_local_cpu(); |
| 294 | } |
| 295 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 296 | static void _ipi_private_reset(void *ignored) |
| 297 | { |
| 298 | int err; |
| 299 | |
| 300 | err = invoke_sdei_fn(SDEI_1_0_FN_SDEI_PRIVATE_RESET, 0, 0, 0, 0, 0, |
| 301 | NULL); |
| 302 | if (err && err != -EIO) |
| 303 | pr_warn_once("failed to reset CPU[%u]: %d\n", |
| 304 | smp_processor_id(), err); |
| 305 | } |
| 306 | |
| 307 | static int sdei_api_shared_reset(void) |
| 308 | { |
| 309 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_SHARED_RESET, 0, 0, 0, 0, 0, |
| 310 | NULL); |
| 311 | } |
| 312 | |
| 313 | static void sdei_mark_interface_broken(void) |
| 314 | { |
| 315 | pr_err("disabling SDEI firmware interface\n"); |
| 316 | on_each_cpu(&_ipi_mask_cpu, NULL, true); |
| 317 | sdei_firmware_call = NULL; |
| 318 | } |
| 319 | |
| 320 | static int sdei_platform_reset(void) |
| 321 | { |
| 322 | int err; |
| 323 | |
| 324 | on_each_cpu(&_ipi_private_reset, NULL, true); |
| 325 | err = sdei_api_shared_reset(); |
| 326 | if (err) { |
| 327 | pr_err("Failed to reset platform: %d\n", err); |
| 328 | sdei_mark_interface_broken(); |
| 329 | } |
| 330 | |
| 331 | return err; |
| 332 | } |
| 333 | |
| 334 | static int sdei_api_event_enable(u32 event_num) |
| 335 | { |
| 336 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_ENABLE, event_num, 0, 0, 0, |
| 337 | 0, NULL); |
| 338 | } |
| 339 | |
| 340 | int sdei_event_enable(u32 event_num) |
| 341 | { |
| 342 | int err = -EINVAL; |
| 343 | struct sdei_event *event; |
| 344 | |
| 345 | mutex_lock(&sdei_events_lock); |
| 346 | event = sdei_event_find(event_num); |
| 347 | if (!event) { |
| 348 | mutex_unlock(&sdei_events_lock); |
| 349 | return -ENOENT; |
| 350 | } |
| 351 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 352 | spin_lock(&sdei_list_lock); |
| 353 | event->reenable = true; |
| 354 | spin_unlock(&sdei_list_lock); |
| 355 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 356 | if (event->type == SDEI_EVENT_TYPE_SHARED) |
| 357 | err = sdei_api_event_enable(event->event_num); |
| 358 | mutex_unlock(&sdei_events_lock); |
| 359 | |
| 360 | return err; |
| 361 | } |
| 362 | EXPORT_SYMBOL(sdei_event_enable); |
| 363 | |
| 364 | static int sdei_api_event_disable(u32 event_num) |
| 365 | { |
| 366 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_DISABLE, event_num, 0, 0, |
| 367 | 0, 0, NULL); |
| 368 | } |
| 369 | |
| 370 | int sdei_event_disable(u32 event_num) |
| 371 | { |
| 372 | int err = -EINVAL; |
| 373 | struct sdei_event *event; |
| 374 | |
| 375 | mutex_lock(&sdei_events_lock); |
| 376 | event = sdei_event_find(event_num); |
| 377 | if (!event) { |
| 378 | mutex_unlock(&sdei_events_lock); |
| 379 | return -ENOENT; |
| 380 | } |
| 381 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 382 | spin_lock(&sdei_list_lock); |
| 383 | event->reenable = false; |
| 384 | spin_unlock(&sdei_list_lock); |
| 385 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 386 | if (event->type == SDEI_EVENT_TYPE_SHARED) |
| 387 | err = sdei_api_event_disable(event->event_num); |
| 388 | mutex_unlock(&sdei_events_lock); |
| 389 | |
| 390 | return err; |
| 391 | } |
| 392 | EXPORT_SYMBOL(sdei_event_disable); |
| 393 | |
| 394 | static int sdei_api_event_unregister(u32 event_num) |
| 395 | { |
| 396 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_UNREGISTER, event_num, 0, |
| 397 | 0, 0, 0, NULL); |
| 398 | } |
| 399 | |
| 400 | static int _sdei_event_unregister(struct sdei_event *event) |
| 401 | { |
| 402 | lockdep_assert_held(&sdei_events_lock); |
| 403 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 404 | spin_lock(&sdei_list_lock); |
| 405 | event->reregister = false; |
| 406 | event->reenable = false; |
| 407 | spin_unlock(&sdei_list_lock); |
| 408 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 409 | if (event->type == SDEI_EVENT_TYPE_SHARED) |
| 410 | return sdei_api_event_unregister(event->event_num); |
| 411 | |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | |
| 415 | int sdei_event_unregister(u32 event_num) |
| 416 | { |
| 417 | int err; |
| 418 | struct sdei_event *event; |
| 419 | |
| 420 | WARN_ON(in_nmi()); |
| 421 | |
| 422 | mutex_lock(&sdei_events_lock); |
| 423 | event = sdei_event_find(event_num); |
| 424 | do { |
| 425 | if (!event) { |
| 426 | pr_warn("Event %u not registered\n", event_num); |
| 427 | err = -ENOENT; |
| 428 | break; |
| 429 | } |
| 430 | |
| 431 | err = _sdei_event_unregister(event); |
| 432 | if (err) |
| 433 | break; |
| 434 | |
| 435 | sdei_event_destroy(event); |
| 436 | } while (0); |
| 437 | mutex_unlock(&sdei_events_lock); |
| 438 | |
| 439 | return err; |
| 440 | } |
| 441 | EXPORT_SYMBOL(sdei_event_unregister); |
| 442 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 443 | /* |
| 444 | * unregister events, but don't destroy them as they are re-registered by |
| 445 | * sdei_reregister_shared(). |
| 446 | */ |
| 447 | static int sdei_unregister_shared(void) |
| 448 | { |
| 449 | int err = 0; |
| 450 | struct sdei_event *event; |
| 451 | |
| 452 | mutex_lock(&sdei_events_lock); |
| 453 | spin_lock(&sdei_list_lock); |
| 454 | list_for_each_entry(event, &sdei_list, list) { |
| 455 | if (event->type != SDEI_EVENT_TYPE_SHARED) |
| 456 | continue; |
| 457 | |
| 458 | err = _sdei_event_unregister(event); |
| 459 | if (err) |
| 460 | break; |
| 461 | } |
| 462 | spin_unlock(&sdei_list_lock); |
| 463 | mutex_unlock(&sdei_events_lock); |
| 464 | |
| 465 | return err; |
| 466 | } |
| 467 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 468 | static int sdei_api_event_register(u32 event_num, unsigned long entry_point, |
| 469 | void *arg, u64 flags, u64 affinity) |
| 470 | { |
| 471 | return invoke_sdei_fn(SDEI_1_0_FN_SDEI_EVENT_REGISTER, event_num, |
| 472 | (unsigned long)entry_point, (unsigned long)arg, |
| 473 | flags, affinity, NULL); |
| 474 | } |
| 475 | |
| 476 | static int _sdei_event_register(struct sdei_event *event) |
| 477 | { |
| 478 | lockdep_assert_held(&sdei_events_lock); |
| 479 | |
| 480 | if (event->type == SDEI_EVENT_TYPE_SHARED) |
| 481 | return sdei_api_event_register(event->event_num, |
| 482 | sdei_entry_point, |
| 483 | event->registered, |
| 484 | SDEI_EVENT_REGISTER_RM_ANY, 0); |
| 485 | |
| 486 | return -EINVAL; |
| 487 | } |
| 488 | |
| 489 | int sdei_event_register(u32 event_num, sdei_event_callback *cb, void *arg) |
| 490 | { |
| 491 | int err; |
| 492 | struct sdei_event *event; |
| 493 | |
| 494 | WARN_ON(in_nmi()); |
| 495 | |
| 496 | mutex_lock(&sdei_events_lock); |
| 497 | do { |
| 498 | if (sdei_event_find(event_num)) { |
| 499 | pr_warn("Event %u already registered\n", event_num); |
| 500 | err = -EBUSY; |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | event = sdei_event_create(event_num, cb, arg); |
| 505 | if (IS_ERR(event)) { |
| 506 | err = PTR_ERR(event); |
| 507 | pr_warn("Failed to create event %u: %d\n", event_num, |
| 508 | err); |
| 509 | break; |
| 510 | } |
| 511 | |
| 512 | err = _sdei_event_register(event); |
| 513 | if (err) { |
| 514 | sdei_event_destroy(event); |
| 515 | pr_warn("Failed to register event %u: %d\n", event_num, |
| 516 | err); |
| 517 | } |
| 518 | } while (0); |
| 519 | mutex_unlock(&sdei_events_lock); |
| 520 | |
| 521 | return err; |
| 522 | } |
| 523 | EXPORT_SYMBOL(sdei_event_register); |
| 524 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 525 | static int sdei_reregister_event(struct sdei_event *event) |
| 526 | { |
| 527 | int err; |
| 528 | |
| 529 | lockdep_assert_held(&sdei_events_lock); |
| 530 | |
| 531 | err = _sdei_event_register(event); |
| 532 | if (err) { |
| 533 | pr_err("Failed to re-register event %u\n", event->event_num); |
| 534 | sdei_event_destroy(event); |
| 535 | return err; |
| 536 | } |
| 537 | |
| 538 | if (event->reenable) { |
| 539 | if (event->type == SDEI_EVENT_TYPE_SHARED) |
| 540 | err = sdei_api_event_enable(event->event_num); |
| 541 | } |
| 542 | |
| 543 | if (err) |
| 544 | pr_err("Failed to re-enable event %u\n", event->event_num); |
| 545 | |
| 546 | return err; |
| 547 | } |
| 548 | |
| 549 | static int sdei_reregister_shared(void) |
| 550 | { |
| 551 | int err = 0; |
| 552 | struct sdei_event *event; |
| 553 | |
| 554 | mutex_lock(&sdei_events_lock); |
| 555 | spin_lock(&sdei_list_lock); |
| 556 | list_for_each_entry(event, &sdei_list, list) { |
| 557 | if (event->type != SDEI_EVENT_TYPE_SHARED) |
| 558 | continue; |
| 559 | |
| 560 | if (event->reregister) { |
| 561 | err = sdei_reregister_event(event); |
| 562 | if (err) |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | spin_unlock(&sdei_list_lock); |
| 567 | mutex_unlock(&sdei_events_lock); |
| 568 | |
| 569 | return err; |
| 570 | } |
| 571 | |
| 572 | /* When entering idle, mask/unmask events for this cpu */ |
| 573 | static int sdei_pm_notifier(struct notifier_block *nb, unsigned long action, |
| 574 | void *data) |
| 575 | { |
| 576 | int rv; |
| 577 | |
| 578 | switch (action) { |
| 579 | case CPU_PM_ENTER: |
| 580 | rv = sdei_mask_local_cpu(); |
| 581 | break; |
| 582 | case CPU_PM_EXIT: |
| 583 | case CPU_PM_ENTER_FAILED: |
| 584 | rv = sdei_unmask_local_cpu(); |
| 585 | break; |
| 586 | default: |
| 587 | return NOTIFY_DONE; |
| 588 | } |
| 589 | |
| 590 | if (rv) |
| 591 | return notifier_from_errno(rv); |
| 592 | |
| 593 | return NOTIFY_OK; |
| 594 | } |
| 595 | |
| 596 | static struct notifier_block sdei_pm_nb = { |
| 597 | .notifier_call = sdei_pm_notifier, |
| 598 | }; |
| 599 | |
| 600 | static int sdei_device_suspend(struct device *dev) |
| 601 | { |
| 602 | on_each_cpu(_ipi_mask_cpu, NULL, true); |
| 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | static int sdei_device_resume(struct device *dev) |
| 608 | { |
| 609 | on_each_cpu(_ipi_unmask_cpu, NULL, true); |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * We need all events to be reregistered when we resume from hibernate. |
| 616 | * |
| 617 | * The sequence is freeze->thaw. Reboot. freeze->restore. We unregister |
| 618 | * events during freeze, then re-register and re-enable them during thaw |
| 619 | * and restore. |
| 620 | */ |
| 621 | static int sdei_device_freeze(struct device *dev) |
| 622 | { |
| 623 | int err; |
| 624 | |
| 625 | cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); |
| 626 | |
| 627 | err = sdei_unregister_shared(); |
| 628 | if (err) |
| 629 | return err; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static int sdei_device_thaw(struct device *dev) |
| 635 | { |
| 636 | int err; |
| 637 | |
| 638 | /* re-register shared events */ |
| 639 | err = sdei_reregister_shared(); |
| 640 | if (err) { |
| 641 | pr_warn("Failed to re-register shared events...\n"); |
| 642 | sdei_mark_interface_broken(); |
| 643 | return err; |
| 644 | } |
| 645 | |
| 646 | err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", |
| 647 | &sdei_cpuhp_up, &sdei_cpuhp_down); |
| 648 | if (err) |
| 649 | pr_warn("Failed to re-register CPU hotplug notifier...\n"); |
| 650 | |
| 651 | return err; |
| 652 | } |
| 653 | |
| 654 | static int sdei_device_restore(struct device *dev) |
| 655 | { |
| 656 | int err; |
| 657 | |
| 658 | err = sdei_platform_reset(); |
| 659 | if (err) |
| 660 | return err; |
| 661 | |
| 662 | return sdei_device_thaw(dev); |
| 663 | } |
| 664 | |
| 665 | static const struct dev_pm_ops sdei_pm_ops = { |
| 666 | .suspend = sdei_device_suspend, |
| 667 | .resume = sdei_device_resume, |
| 668 | .freeze = sdei_device_freeze, |
| 669 | .thaw = sdei_device_thaw, |
| 670 | .restore = sdei_device_restore, |
| 671 | }; |
| 672 | |
| 673 | /* |
| 674 | * Mask all CPUs and unregister all events on panic, reboot or kexec. |
| 675 | */ |
| 676 | static int sdei_reboot_notifier(struct notifier_block *nb, unsigned long action, |
| 677 | void *data) |
| 678 | { |
| 679 | /* |
| 680 | * We are going to reset the interface, after this there is no point |
| 681 | * doing work when we take CPUs offline. |
| 682 | */ |
| 683 | cpuhp_remove_state(CPUHP_AP_ARM_SDEI_STARTING); |
| 684 | |
| 685 | sdei_platform_reset(); |
| 686 | |
| 687 | return NOTIFY_OK; |
| 688 | } |
| 689 | |
| 690 | static struct notifier_block sdei_reboot_nb = { |
| 691 | .notifier_call = sdei_reboot_notifier, |
| 692 | }; |
| 693 | |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 694 | static void sdei_smccc_smc(unsigned long function_id, |
| 695 | unsigned long arg0, unsigned long arg1, |
| 696 | unsigned long arg2, unsigned long arg3, |
| 697 | unsigned long arg4, struct arm_smccc_res *res) |
| 698 | { |
| 699 | arm_smccc_smc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); |
| 700 | } |
| 701 | |
| 702 | static void sdei_smccc_hvc(unsigned long function_id, |
| 703 | unsigned long arg0, unsigned long arg1, |
| 704 | unsigned long arg2, unsigned long arg3, |
| 705 | unsigned long arg4, struct arm_smccc_res *res) |
| 706 | { |
| 707 | arm_smccc_hvc(function_id, arg0, arg1, arg2, arg3, arg4, 0, 0, res); |
| 708 | } |
| 709 | |
| 710 | static int sdei_get_conduit(struct platform_device *pdev) |
| 711 | { |
| 712 | const char *method; |
| 713 | struct device_node *np = pdev->dev.of_node; |
| 714 | |
| 715 | sdei_firmware_call = NULL; |
| 716 | if (np) { |
| 717 | if (of_property_read_string(np, "method", &method)) { |
| 718 | pr_warn("missing \"method\" property\n"); |
| 719 | return CONDUIT_INVALID; |
| 720 | } |
| 721 | |
| 722 | if (!strcmp("hvc", method)) { |
| 723 | sdei_firmware_call = &sdei_smccc_hvc; |
| 724 | return CONDUIT_HVC; |
| 725 | } else if (!strcmp("smc", method)) { |
| 726 | sdei_firmware_call = &sdei_smccc_smc; |
| 727 | return CONDUIT_SMC; |
| 728 | } |
| 729 | |
| 730 | pr_warn("invalid \"method\" property: %s\n", method); |
| 731 | } |
| 732 | |
| 733 | return CONDUIT_INVALID; |
| 734 | } |
| 735 | |
| 736 | static int sdei_probe(struct platform_device *pdev) |
| 737 | { |
| 738 | int err; |
| 739 | u64 ver = 0; |
| 740 | int conduit; |
| 741 | |
| 742 | conduit = sdei_get_conduit(pdev); |
| 743 | if (!sdei_firmware_call) |
| 744 | return 0; |
| 745 | |
| 746 | err = sdei_api_get_version(&ver); |
| 747 | if (err == -EOPNOTSUPP) |
| 748 | pr_err("advertised but not implemented in platform firmware\n"); |
| 749 | if (err) { |
| 750 | pr_err("Failed to get SDEI version: %d\n", err); |
| 751 | sdei_mark_interface_broken(); |
| 752 | return err; |
| 753 | } |
| 754 | |
| 755 | pr_info("SDEIv%d.%d (0x%x) detected in firmware.\n", |
| 756 | (int)SDEI_VERSION_MAJOR(ver), (int)SDEI_VERSION_MINOR(ver), |
| 757 | (int)SDEI_VERSION_VENDOR(ver)); |
| 758 | |
| 759 | if (SDEI_VERSION_MAJOR(ver) != 1) { |
| 760 | pr_warn("Conflicting SDEI version detected.\n"); |
| 761 | sdei_mark_interface_broken(); |
| 762 | return -EINVAL; |
| 763 | } |
| 764 | |
| 765 | err = sdei_platform_reset(); |
| 766 | if (err) |
| 767 | return err; |
| 768 | |
| 769 | sdei_entry_point = sdei_arch_get_entry_point(conduit); |
| 770 | if (!sdei_entry_point) { |
| 771 | /* Not supported due to hardware or boot configuration */ |
| 772 | sdei_mark_interface_broken(); |
| 773 | return 0; |
| 774 | } |
| 775 | |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 776 | err = cpu_pm_register_notifier(&sdei_pm_nb); |
| 777 | if (err) { |
| 778 | pr_warn("Failed to register CPU PM notifier...\n"); |
| 779 | goto error; |
| 780 | } |
| 781 | |
| 782 | err = register_reboot_notifier(&sdei_reboot_nb); |
| 783 | if (err) { |
| 784 | pr_warn("Failed to register reboot notifier...\n"); |
| 785 | goto remove_cpupm; |
| 786 | } |
| 787 | |
| 788 | err = cpuhp_setup_state(CPUHP_AP_ARM_SDEI_STARTING, "SDEI", |
| 789 | &sdei_cpuhp_up, &sdei_cpuhp_down); |
| 790 | if (err) { |
| 791 | pr_warn("Failed to register CPU hotplug notifier...\n"); |
| 792 | goto remove_reboot; |
| 793 | } |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 794 | |
| 795 | return 0; |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 796 | |
| 797 | remove_reboot: |
| 798 | unregister_reboot_notifier(&sdei_reboot_nb); |
| 799 | |
| 800 | remove_cpupm: |
| 801 | cpu_pm_unregister_notifier(&sdei_pm_nb); |
| 802 | |
| 803 | error: |
| 804 | sdei_mark_interface_broken(); |
| 805 | return err; |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static const struct of_device_id sdei_of_match[] = { |
| 809 | { .compatible = "arm,sdei-1.0" }, |
| 810 | {} |
| 811 | }; |
| 812 | |
| 813 | static struct platform_driver sdei_driver = { |
| 814 | .driver = { |
| 815 | .name = "sdei", |
James Morse | da35182 | 2018-01-08 15:38:13 +0000 | [diff] [blame^] | 816 | .pm = &sdei_pm_ops, |
James Morse | ad6eb31 | 2018-01-08 15:38:09 +0000 | [diff] [blame] | 817 | .of_match_table = sdei_of_match, |
| 818 | }, |
| 819 | .probe = sdei_probe, |
| 820 | }; |
| 821 | |
| 822 | static bool __init sdei_present_dt(void) |
| 823 | { |
| 824 | struct platform_device *pdev; |
| 825 | struct device_node *np, *fw_np; |
| 826 | |
| 827 | fw_np = of_find_node_by_name(NULL, "firmware"); |
| 828 | if (!fw_np) |
| 829 | return false; |
| 830 | |
| 831 | np = of_find_matching_node(fw_np, sdei_of_match); |
| 832 | of_node_put(fw_np); |
| 833 | if (!np) |
| 834 | return false; |
| 835 | |
| 836 | pdev = of_platform_device_create(np, sdei_driver.driver.name, NULL); |
| 837 | of_node_put(np); |
| 838 | if (IS_ERR(pdev)) |
| 839 | return false; |
| 840 | |
| 841 | return true; |
| 842 | } |
| 843 | |
| 844 | static int __init sdei_init(void) |
| 845 | { |
| 846 | if (sdei_present_dt()) |
| 847 | platform_driver_register(&sdei_driver); |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | subsys_initcall_sync(sdei_init); |
| 853 | |
| 854 | int sdei_event_handler(struct pt_regs *regs, |
| 855 | struct sdei_registered_event *arg) |
| 856 | { |
| 857 | int err; |
| 858 | mm_segment_t orig_addr_limit; |
| 859 | u32 event_num = arg->event_num; |
| 860 | |
| 861 | orig_addr_limit = get_fs(); |
| 862 | set_fs(USER_DS); |
| 863 | |
| 864 | err = arg->callback(event_num, regs, arg->callback_arg); |
| 865 | if (err) |
| 866 | pr_err_ratelimited("event %u on CPU %u failed with error: %d\n", |
| 867 | event_num, smp_processor_id(), err); |
| 868 | |
| 869 | set_fs(orig_addr_limit); |
| 870 | |
| 871 | return err; |
| 872 | } |
| 873 | NOKPROBE_SYMBOL(sdei_event_handler); |