mmu_context.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2012 Regents of the University of California
  3. * Copyright (C) 2017 SiFive
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation, version 2.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef _ASM_RISCV_MMU_CONTEXT_H
  15. #define _ASM_RISCV_MMU_CONTEXT_H
  16. #include <linux/mm_types.h>
  17. #include <asm-generic/mm_hooks.h>
  18. #include <linux/mm.h>
  19. #include <linux/sched.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/cacheflush.h>
  22. static inline void enter_lazy_tlb(struct mm_struct *mm,
  23. struct task_struct *task)
  24. {
  25. }
  26. /* Initialize context-related info for a new mm_struct */
  27. static inline int init_new_context(struct task_struct *task,
  28. struct mm_struct *mm)
  29. {
  30. return 0;
  31. }
  32. static inline void destroy_context(struct mm_struct *mm)
  33. {
  34. }
  35. /*
  36. * When necessary, performs a deferred icache flush for the given MM context,
  37. * on the local CPU. RISC-V has no direct mechanism for instruction cache
  38. * shoot downs, so instead we send an IPI that informs the remote harts they
  39. * need to flush their local instruction caches. To avoid pathologically slow
  40. * behavior in a common case (a bunch of single-hart processes on a many-hart
  41. * machine, ie 'make -j') we avoid the IPIs for harts that are not currently
  42. * executing a MM context and instead schedule a deferred local instruction
  43. * cache flush to be performed before execution resumes on each hart. This
  44. * actually performs that local instruction cache flush, which implicitly only
  45. * refers to the current hart.
  46. */
  47. static inline void flush_icache_deferred(struct mm_struct *mm)
  48. {
  49. #ifdef CONFIG_SMP
  50. unsigned int cpu = smp_processor_id();
  51. cpumask_t *mask = &mm->context.icache_stale_mask;
  52. if (cpumask_test_cpu(cpu, mask)) {
  53. cpumask_clear_cpu(cpu, mask);
  54. /*
  55. * Ensure the remote hart's writes are visible to this hart.
  56. * This pairs with a barrier in flush_icache_mm.
  57. */
  58. smp_mb();
  59. local_flush_icache_all();
  60. }
  61. #endif
  62. }
  63. static inline void switch_mm(struct mm_struct *prev,
  64. struct mm_struct *next, struct task_struct *task)
  65. {
  66. if (likely(prev != next)) {
  67. /*
  68. * Mark the current MM context as inactive, and the next as
  69. * active. This is at least used by the icache flushing
  70. * routines in order to determine who should
  71. */
  72. unsigned int cpu = smp_processor_id();
  73. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  74. cpumask_set_cpu(cpu, mm_cpumask(next));
  75. /*
  76. * Use the old spbtr name instead of using the current satp
  77. * name to support binutils 2.29 which doesn't know about the
  78. * privileged ISA 1.10 yet.
  79. */
  80. csr_write(sptbr, virt_to_pfn(next->pgd) | SATP_MODE);
  81. local_flush_tlb_all();
  82. flush_icache_deferred(next);
  83. }
  84. }
  85. static inline void activate_mm(struct mm_struct *prev,
  86. struct mm_struct *next)
  87. {
  88. switch_mm(prev, next, NULL);
  89. }
  90. static inline void deactivate_mm(struct task_struct *task,
  91. struct mm_struct *mm)
  92. {
  93. }
  94. #endif /* _ASM_RISCV_MMU_CONTEXT_H */