suspend.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021 Western Digital Corporation or its affiliates.
  4. * Copyright (c) 2022 Ventana Micro Systems Inc.
  5. */
  6. #include <linux/ftrace.h>
  7. #include <asm/csr.h>
  8. #include <asm/suspend.h>
  9. static void suspend_save_csrs(struct suspend_context *context)
  10. {
  11. context->scratch = csr_read(CSR_SCRATCH);
  12. context->tvec = csr_read(CSR_TVEC);
  13. context->ie = csr_read(CSR_IE);
  14. /*
  15. * No need to save/restore IP CSR (i.e. MIP or SIP) because:
  16. *
  17. * 1. For no-MMU (M-mode) kernel, the bits in MIP are set by
  18. * external devices (such as interrupt controller, timer, etc).
  19. * 2. For MMU (S-mode) kernel, the bits in SIP are set by
  20. * M-mode firmware and external devices (such as interrupt
  21. * controller, etc).
  22. */
  23. #ifdef CONFIG_MMU
  24. context->satp = csr_read(CSR_SATP);
  25. #endif
  26. }
  27. static void suspend_restore_csrs(struct suspend_context *context)
  28. {
  29. csr_write(CSR_SCRATCH, context->scratch);
  30. csr_write(CSR_TVEC, context->tvec);
  31. csr_write(CSR_IE, context->ie);
  32. #ifdef CONFIG_MMU
  33. csr_write(CSR_SATP, context->satp);
  34. #endif
  35. }
  36. int cpu_suspend(unsigned long arg,
  37. int (*finish)(unsigned long arg,
  38. unsigned long entry,
  39. unsigned long context))
  40. {
  41. int rc = 0;
  42. struct suspend_context context = { 0 };
  43. /* Finisher should be non-NULL */
  44. if (!finish)
  45. return -EINVAL;
  46. /* Save additional CSRs*/
  47. suspend_save_csrs(&context);
  48. /*
  49. * Function graph tracer state gets incosistent when the kernel
  50. * calls functions that never return (aka finishers) hence disable
  51. * graph tracing during their execution.
  52. */
  53. pause_graph_tracing();
  54. /* Save context on stack */
  55. if (__cpu_suspend_enter(&context)) {
  56. /* Call the finisher */
  57. rc = finish(arg, __pa_symbol(__cpu_resume_enter),
  58. (ulong)&context);
  59. /*
  60. * Should never reach here, unless the suspend finisher
  61. * fails. Successful cpu_suspend() should return from
  62. * __cpu_resume_entry()
  63. */
  64. if (!rc)
  65. rc = -EOPNOTSUPP;
  66. }
  67. /* Enable function graph tracer */
  68. unpause_graph_tracing();
  69. /* Restore additional CSRs */
  70. suspend_restore_csrs(&context);
  71. return rc;
  72. }