mmu_context.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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)
  44. {
  45. u64 asid = asid_cache(cpu);
  46. if (!((++asid) & cpu_asid_mask(&cpu_data[cpu])))
  47. local_flush_tlb_user(); /* 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 switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
  63. struct task_struct *tsk)
  64. {
  65. unsigned int cpu = smp_processor_id();
  66. /* Check if our ASID is of an older version and thus invalid */
  67. if (!asid_valid(next, cpu))
  68. get_new_mmu_context(next, cpu);
  69. write_csr_asid(cpu_asid(cpu, next));
  70. if (next != &init_mm)
  71. csr_write64((unsigned long)next->pgd, LOONGARCH_CSR_PGDL);
  72. else
  73. csr_write64((unsigned long)invalid_pg_dir, LOONGARCH_CSR_PGDL);
  74. /*
  75. * Mark current->active_mm as not "active" anymore.
  76. * We don't want to mislead possible IPI tlb flush routines.
  77. */
  78. cpumask_set_cpu(cpu, mm_cpumask(next));
  79. }
  80. #define switch_mm_irqs_off switch_mm_irqs_off
  81. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  82. struct task_struct *tsk)
  83. {
  84. unsigned long flags;
  85. local_irq_save(flags);
  86. switch_mm_irqs_off(prev, next, tsk);
  87. local_irq_restore(flags);
  88. }
  89. /*
  90. * Destroy context related info for an mm_struct that is about
  91. * to be put to rest.
  92. */
  93. static inline void destroy_context(struct mm_struct *mm)
  94. {
  95. }
  96. #define activate_mm(prev, next) switch_mm(prev, next, current)
  97. #define deactivate_mm(task, mm) do { } while (0)
  98. /*
  99. * If mm is currently active, we can't really drop it.
  100. * Instead, we will get a new one for it.
  101. */
  102. static inline void
  103. drop_mmu_context(struct mm_struct *mm, unsigned int cpu)
  104. {
  105. int asid;
  106. unsigned long flags;
  107. local_irq_save(flags);
  108. asid = read_csr_asid() & cpu_asid_mask(&current_cpu_data);
  109. if (asid == cpu_asid(cpu, mm)) {
  110. if (!current->mm || (current->mm == mm)) {
  111. get_new_mmu_context(mm, cpu);
  112. write_csr_asid(cpu_asid(cpu, mm));
  113. goto out;
  114. }
  115. }
  116. /* Will get a new context next time */
  117. cpu_context(cpu, mm) = 0;
  118. cpumask_clear_cpu(cpu, mm_cpumask(mm));
  119. out:
  120. local_irq_restore(flags);
  121. }
  122. #endif /* _ASM_MMU_CONTEXT_H */