tlb.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * OpenRISC tlb.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Julius Baxter <julius.baxter@orsoc.se>
  11. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/mman.h>
  25. #include <linux/mm.h>
  26. #include <linux/init.h>
  27. #include <asm/segment.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/mmu_context.h>
  31. #include <asm/spr_defs.h>
  32. #define NO_CONTEXT -1
  33. #define NUM_DTLB_SETS (1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> \
  34. SPR_DMMUCFGR_NTS_OFF))
  35. #define NUM_ITLB_SETS (1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> \
  36. SPR_IMMUCFGR_NTS_OFF))
  37. #define DTLB_OFFSET(addr) (((addr) >> PAGE_SHIFT) & (NUM_DTLB_SETS-1))
  38. #define ITLB_OFFSET(addr) (((addr) >> PAGE_SHIFT) & (NUM_ITLB_SETS-1))
  39. /*
  40. * Invalidate all TLB entries.
  41. *
  42. * This comes down to setting the 'valid' bit for all xTLBMR registers to 0.
  43. * Easiest way to accomplish this is to just zero out the xTLBMR register
  44. * completely.
  45. *
  46. */
  47. void local_flush_tlb_all(void)
  48. {
  49. int i;
  50. unsigned long num_tlb_sets;
  51. /* Determine number of sets for IMMU. */
  52. /* FIXME: Assumption is I & D nsets equal. */
  53. num_tlb_sets = NUM_ITLB_SETS;
  54. for (i = 0; i < num_tlb_sets; i++) {
  55. mtspr_off(SPR_DTLBMR_BASE(0), i, 0);
  56. mtspr_off(SPR_ITLBMR_BASE(0), i, 0);
  57. }
  58. }
  59. #define have_dtlbeir (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_TEIRI)
  60. #define have_itlbeir (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_TEIRI)
  61. /*
  62. * Invalidate a single page. This is what the xTLBEIR register is for.
  63. *
  64. * There's no point in checking the vma for PAGE_EXEC to determine whether it's
  65. * the data or instruction TLB that should be flushed... that would take more
  66. * than the few instructions that the following compiles down to!
  67. *
  68. * The case where we don't have the xTLBEIR register really only works for
  69. * MMU's with a single way and is hard-coded that way.
  70. */
  71. #define flush_dtlb_page_eir(addr) mtspr(SPR_DTLBEIR, addr)
  72. #define flush_dtlb_page_no_eir(addr) \
  73. mtspr_off(SPR_DTLBMR_BASE(0), DTLB_OFFSET(addr), 0);
  74. #define flush_itlb_page_eir(addr) mtspr(SPR_ITLBEIR, addr)
  75. #define flush_itlb_page_no_eir(addr) \
  76. mtspr_off(SPR_ITLBMR_BASE(0), ITLB_OFFSET(addr), 0);
  77. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
  78. {
  79. if (have_dtlbeir)
  80. flush_dtlb_page_eir(addr);
  81. else
  82. flush_dtlb_page_no_eir(addr);
  83. if (have_itlbeir)
  84. flush_itlb_page_eir(addr);
  85. else
  86. flush_itlb_page_no_eir(addr);
  87. }
  88. void local_flush_tlb_range(struct vm_area_struct *vma,
  89. unsigned long start, unsigned long end)
  90. {
  91. int addr;
  92. bool dtlbeir;
  93. bool itlbeir;
  94. dtlbeir = have_dtlbeir;
  95. itlbeir = have_itlbeir;
  96. for (addr = start; addr < end; addr += PAGE_SIZE) {
  97. if (dtlbeir)
  98. flush_dtlb_page_eir(addr);
  99. else
  100. flush_dtlb_page_no_eir(addr);
  101. if (itlbeir)
  102. flush_itlb_page_eir(addr);
  103. else
  104. flush_itlb_page_no_eir(addr);
  105. }
  106. }
  107. /*
  108. * Invalidate the selected mm context only.
  109. *
  110. * FIXME: Due to some bug here, we're flushing everything for now.
  111. * This should be changed to loop over over mm and call flush_tlb_range.
  112. */
  113. void local_flush_tlb_mm(struct mm_struct *mm)
  114. {
  115. /* Was seeing bugs with the mm struct passed to us. Scrapped most of
  116. this function. */
  117. /* Several architctures do this */
  118. local_flush_tlb_all();
  119. }
  120. /* called in schedule() just before actually doing the switch_to */
  121. void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  122. struct task_struct *next_tsk)
  123. {
  124. /* remember the pgd for the fault handlers
  125. * this is similar to the pgd register in some other CPU's.
  126. * we need our own copy of it because current and active_mm
  127. * might be invalid at points where we still need to derefer
  128. * the pgd.
  129. */
  130. current_pgd[smp_processor_id()] = next->pgd;
  131. /* We don't have context support implemented, so flush all
  132. * entries belonging to previous map
  133. */
  134. if (prev != next)
  135. local_flush_tlb_mm(prev);
  136. }
  137. /*
  138. * Initialize the context related info for a new mm_struct
  139. * instance.
  140. */
  141. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  142. {
  143. mm->context = NO_CONTEXT;
  144. return 0;
  145. }
  146. /* called by __exit_mm to destroy the used MMU context if any before
  147. * destroying the mm itself. this is only called when the last user of the mm
  148. * drops it.
  149. */
  150. void destroy_context(struct mm_struct *mm)
  151. {
  152. flush_tlb_mm(mm);
  153. }
  154. /* called once during VM initialization, from init.c */
  155. void __init tlb_init(void)
  156. {
  157. /* Do nothing... */
  158. /* invalidate the entire TLB */
  159. /* flush_tlb_all(); */
  160. }