sbi_system.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <sbi/riscv_io.h>
  38. void __noreturn sbi_system_reset(u32 reset_type, u32 reset_reason)
  39. {
  40. ulong hbase = 0, hmask;
  41. u32 cur_hartid = current_hartid();
  42. struct sbi_domain *dom = sbi_domain_thishart_ptr();
  43. struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
  44. /* Send HALT IPI to every hart other than the current hart */
  45. while (!sbi_hsm_hart_interruptible_mask(dom, hbase, &hmask)) {
  46. if (hbase <= cur_hartid)
  47. hmask &= ~(1UL << (cur_hartid - hbase));
  48. if (hmask)
  49. sbi_ipi_send_halt(hmask, hbase);
  50. hbase += BITS_PER_LONG;
  51. }
  52. /* Stop current HART */
  53. sbi_hsm_hart_stop(scratch, FALSE);
  54. /* Platform specific reset if domain allowed system reset */
  55. if (dom->system_reset_allowed &&
  56. reset_dev && reset_dev->system_reset)
  57. reset_dev->system_reset(reset_type, reset_reason);
  58. writew(0x5555, (void *)0x100000); // qemu poweroff
  59. /* If platform specific reset did not work then do sbi_exit() */
  60. sbi_exit(scratch);
  61. }