mmu_context.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <asm-generic/mm_hooks.h>
  17. #include <linux/mm.h>
  18. #include <linux/sched.h>
  19. #include <asm/tlbflush.h>
  20. #include <asm/cacheflush.h>
  21. static inline void enter_lazy_tlb(struct mm_struct *mm,
  22. struct task_struct *task)
  23. {
  24. }
  25. /* Initialize context-related info for a new mm_struct */
  26. static inline int init_new_context(struct task_struct *task,
  27. struct mm_struct *mm)
  28. {
  29. return 0;
  30. }
  31. static inline void destroy_context(struct mm_struct *mm)
  32. {
  33. }
  34. static inline pgd_t *current_pgdir(void)
  35. {
  36. return pfn_to_virt(csr_read(sptbr) & SPTBR_PPN);
  37. }
  38. static inline void set_pgdir(pgd_t *pgd)
  39. {
  40. csr_write(sptbr, virt_to_pfn(pgd) | SPTBR_MODE);
  41. }
  42. /*
  43. * When necessary, performs a deferred icache flush for the given MM context,
  44. * on the local CPU. RISC-V has no direct mechanism for instruction cache
  45. * shoot downs, so instead we send an IPI that informs the remote harts they
  46. * need to flush their local instruction caches. To avoid pathologically slow
  47. * behavior in a common case (a bunch of single-hart processes on a many-hart
  48. * machine, ie 'make -j') we avoid the IPIs for harts that are not currently
  49. * executing a MM context and instead schedule a deferred local instruction
  50. * cache flush to be performed before execution resumes on each hart. This
  51. * actually performs that local instruction cache flush, which implicitly only
  52. * refers to the current hart.
  53. */
  54. static inline void flush_icache_deferred(struct mm_struct *mm)
  55. {
  56. #ifdef CONFIG_SMP
  57. unsigned int cpu = smp_processor_id();
  58. cpumask_t *mask = &mm->context.icache_stale_mask;
  59. if (cpumask_test_cpu(cpu, mask)) {
  60. cpumask_clear_cpu(cpu, mask);
  61. /*
  62. * Ensure the remote hart's writes are visible to this hart.
  63. * This pairs with a barrier in flush_icache_mm.
  64. */
  65. smp_mb();
  66. local_flush_icache_all();
  67. }
  68. #endif
  69. }
  70. static inline void switch_mm(struct mm_struct *prev,
  71. struct mm_struct *next, struct task_struct *task)
  72. {
  73. if (likely(prev != next)) {
  74. /*
  75. * Mark the current MM context as inactive, and the next as
  76. * active. This is at least used by the icache flushing
  77. * routines in order to determine who should
  78. */
  79. unsigned int cpu = smp_processor_id();
  80. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  81. cpumask_set_cpu(cpu, mm_cpumask(next));
  82. set_pgdir(next->pgd);
  83. local_flush_tlb_all();
  84. flush_icache_deferred(next);
  85. }
  86. }
  87. static inline void activate_mm(struct mm_struct *prev,
  88. struct mm_struct *next)
  89. {
  90. switch_mm(prev, next, NULL);
  91. }
  92. static inline void deactivate_mm(struct task_struct *task,
  93. struct mm_struct *mm)
  94. {
  95. }
  96. #endif /* _ASM_RISCV_MMU_CONTEXT_H */