sbi_system.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. * Nick Kossifidis <mick@ics.forth.gr>
  9. */
  10. #include <sbi/riscv_asm.h>
  11. #include <sbi/sbi_bitops.h>
  12. #include <sbi/sbi_domain.h>
  13. #include <sbi/sbi_hart.h>
  14. #include <sbi/sbi_hsm.h>
  15. #include <sbi/sbi_platform.h>
  16. #include <sbi/sbi_system.h>
  17. #include <sbi/sbi_ipi.h>
  18. #include <sbi/sbi_init.h>
  19. static const struct sbi_system_reset_device *reset_dev = NULL;
  20. const struct sbi_system_reset_device *sbi_system_reset_get_device(void)
  21. {
  22. return reset_dev;
  23. }
  24. void sbi_system_reset_set_device(const struct sbi_system_reset_device *dev)
  25. {
  26. if (!dev || reset_dev)
  27. return;
  28. reset_dev = dev;
  29. }
  30. bool sbi_system_reset_supported(u32 reset_type, u32 reset_reason)
  31. {
  32. if (reset_dev && reset_dev->system_reset_check &&
  33. reset_dev->system_reset_check(reset_type, reset_reason))
  34. return TRUE;
  35. return FALSE;
  36. }
  37. void __noreturn sbi_system_reset(u32 reset_type, u32 reset_reason)
  38. {
  39. ulong hbase = 0, hmask;
  40. u32 cur_hartid = current_hartid();
  41. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  42. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  43. /* Send HALT IPI to every hart other than the current hart */
  44. while (!sbi_hsm_hart_interruptible_mask(dom, hbase, &hmask)) {
  45. if (hbase <= cur_hartid)
  46. hmask &= ~(1UL << (cur_hartid - hbase));
  47. if (hmask)
  48. sbi_ipi_send_halt(hmask, hbase);
  49. hbase += BITS_PER_LONG;
  50. }
  51. /* Stop current HART */
  52. sbi_hsm_hart_stop(scratch, FALSE);
  53. /* Platform specific reset if domain allowed system reset */
  54. if (dom->system_reset_allowed &&
  55. reset_dev && reset_dev->system_reset)
  56. reset_dev->system_reset(reset_type, reset_reason);
  57. /* If platform specific reset did not work then do sbi_exit() */
  58. sbi_exit(scratch);
  59. }