switch_to.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #ifndef _ASM_RISCV_SWITCH_TO_H
  6. #define _ASM_RISCV_SWITCH_TO_H
  7. #include <linux/jump_label.h>
  8. #include <linux/sched/task_stack.h>
  9. #include <asm/hwcap.h>
  10. #include <asm/processor.h>
  11. #include <asm/ptrace.h>
  12. #include <asm/csr.h>
  13. #ifdef CONFIG_FPU
  14. extern void __fstate_save(struct task_struct *save_to);
  15. extern void __fstate_restore(struct task_struct *restore_from);
  16. static inline void __fstate_clean(struct pt_regs *regs)
  17. {
  18. regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
  19. }
  20. static inline void fstate_off(struct task_struct *task,
  21. struct pt_regs *regs)
  22. {
  23. regs->status = (regs->status & ~SR_FS) | SR_FS_OFF;
  24. }
  25. static inline void fstate_save(struct task_struct *task,
  26. struct pt_regs *regs)
  27. {
  28. if ((regs->status & SR_FS) == SR_FS_DIRTY) {
  29. __fstate_save(task);
  30. __fstate_clean(regs);
  31. }
  32. }
  33. static inline void fstate_restore(struct task_struct *task,
  34. struct pt_regs *regs)
  35. {
  36. if ((regs->status & SR_FS) != SR_FS_OFF) {
  37. __fstate_restore(task);
  38. __fstate_clean(regs);
  39. }
  40. }
  41. static inline void __switch_to_aux(struct task_struct *prev,
  42. struct task_struct *next)
  43. {
  44. struct pt_regs *regs;
  45. regs = task_pt_regs(prev);
  46. if (unlikely(regs->status & SR_SD))
  47. fstate_save(prev, regs);
  48. fstate_restore(next, task_pt_regs(next));
  49. }
  50. static __always_inline bool has_fpu(void)
  51. {
  52. return static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_FPU]);
  53. }
  54. #else
  55. static __always_inline bool has_fpu(void) { return false; }
  56. #define fstate_save(task, regs) do { } while (0)
  57. #define fstate_restore(task, regs) do { } while (0)
  58. #define __switch_to_aux(__prev, __next) do { } while (0)
  59. #endif
  60. extern struct task_struct *__switch_to(struct task_struct *,
  61. struct task_struct *);
  62. #define switch_to(prev, next, last) \
  63. do { \
  64. struct task_struct *__prev = (prev); \
  65. struct task_struct *__next = (next); \
  66. if (has_fpu()) \
  67. __switch_to_aux(__prev, __next); \
  68. ((last) = __switch_to(__prev, __next)); \
  69. } while (0)
  70. #endif /* _ASM_RISCV_SWITCH_TO_H */