mmu_context.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Switch a MMU context.
  4. *
  5. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  6. */
  7. #ifndef _ASM_MMU_CONTEXT_H
  8. #define _ASM_MMU_CONTEXT_H
  9. #include <linux/errno.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm_types.h>
  12. #include <linux/smp.h>
  13. #include <linux/slab.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/tlbflush.h>
  16. #include <asm-generic/mm_hooks.h>
  17. /*
  18. * All unused by hardware upper bits will be considered
  19. * as a software asid extension.
  20. */
  21. static inline u64 asid_version_mask(unsigned int cpu)
  22. {
  23. return ~(u64)(cpu_asid_mask(&cpu_data[cpu]));
  24. }
  25. static inline u64 asid_first_version(unsigned int cpu)
  26. {
  27. return cpu_asid_mask(&cpu_data[cpu]) + 1;
  28. }
  29. #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
  30. #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
  31. #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & cpu_asid_mask(&cpu_data[cpu]))
  32. static inline int asid_valid(struct mm_struct *mm, unsigned int cpu)
  33. {
  34. if ((cpu_context(cpu, mm) ^ asid_cache(cpu)) & asid_version_mask(cpu))
  35. return 0;
  36. return 1;
  37. }
  38. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  39. {
  40. }
  41. /* Normal, classic get_new_mmu_context */
  42. static inline void
  43. get_new_mmu_context(struct mm_struct *mm, unsigned long cpu, bool *need_flush)
  44. {
  45. u64 asid = asid_cache(cpu);
  46. if (!((++asid) & cpu_asid_mask(&cpu_data[cpu])))
  47. *need_flush = true; /* start new asid cycle */
  48. cpu_context(cpu, mm) = asid_cache(cpu) = asid;
  49. }
  50. /*
  51. * Initialize the context related info for a new mm_struct
  52. * instance.
  53. */
  54. static inline int
  55. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  56. {
  57. int i;
  58. for_each_possible_cpu(i)
  59. cpu_context(i, mm) = 0;
  60. return 0;
  61. }
  62. static inline void atomic_update_pgd_asid(unsigned long asid, unsigned long pgdl)
  63. {
  64. __asm__ __volatile__(
  65. "csrwr %[pgdl_val], %[pgdl_reg] \n\t"
  66. "csrwr %[asid_val], %[asid_reg] \n\t"
  67. : [asid_val] "+r" (asid), [pgdl_val] "+r" (pgdl)
  68. : [asid_reg] "i" (LOONGARCH_CSR_ASID), [pgdl_reg] "i" (LOONGARCH_CSR_PGDL)
  69. : "memory"
  70. );
  71. }
  72. static inline void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  73. struct task_struct *tsk)
  74. {
  75. bool need_flush = false;
  76. unsigned int cpu = smp_processor_id();
  77. /* Check if our ASID is of an older version and thus invalid */
  78. if (!asid_valid(next, cpu))
  79. get_new_mmu_context(next, cpu, &need_flush);
  80. if (next != &init_mm)
  81. atomic_update_pgd_asid(cpu_asid(cpu, next), (unsigned long)next->pgd);
  82. else
  83. atomic_update_pgd_asid(cpu_asid(cpu, next), (unsigned long)invalid_pg_dir);
  84. if (need_flush)
  85. local_flush_tlb_user(); /* Flush tlb after update ASID */
  86. /*
  87. * Mark current->active_mm as not "active" anymore.
  88. * We don't want to mislead possible IPI tlb flush routines.
  89. */
  90. cpumask_set_cpu(cpu, mm_cpumask(next));
  91. }
  92. #define switch_mm_irqs_off switch_mm_irqs_off
  93. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  94. struct task_struct *tsk)
  95. {
  96. unsigned long flags;
  97. local_irq_save(flags);
  98. switch_mm_irqs_off(prev, next, tsk);
  99. local_irq_restore(flags);
  100. }
  101. /*
  102. * Destroy context related info for an mm_struct that is about
  103. * to be put to rest.
  104. */
  105. static inline void destroy_context(struct mm_struct *mm)
  106. {
  107. }
  108. #define activate_mm(prev, next) switch_mm(prev, next, current)
  109. #define deactivate_mm(task, mm) do { } while (0)
  110. /*
  111. * If mm is currently active, we can't really drop it.
  112. * Instead, we will get a new one for it.
  113. */
  114. static inline void
  115. drop_mmu_context(struct mm_struct *mm, unsigned int cpu)
  116. {
  117. int asid;
  118. unsigned long flags;
  119. local_irq_save(flags);
  120. asid = read_csr_asid() & cpu_asid_mask(&current_cpu_data);
  121. if (asid == cpu_asid(cpu, mm)) {
  122. bool need_flush = false;
  123. if (!current->mm || (current->mm == mm)) {
  124. get_new_mmu_context(mm, cpu, &need_flush);
  125. write_csr_asid(cpu_asid(cpu, mm));
  126. if (need_flush)
  127. local_flush_tlb_user(); /* Flush tlb after update ASID */
  128. goto out;
  129. }
  130. }
  131. /* Will get a new context next time */
  132. cpu_context(cpu, mm) = 0;
  133. cpumask_clear_cpu(cpu, mm_cpumask(mm));
  134. out:
  135. local_irq_restore(flags);
  136. }
  137. #endif /* _ASM_MMU_CONTEXT_H */