arm_sdei.c 23 KB

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