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/system.h>
  28. #include <asm/segment.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/spr_defs.h>
  33. #define NO_CONTEXT -1
  34. #define NUM_DTLB_SETS (1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> \
  35. SPR_DMMUCFGR_NTS_OFF))
  36. #define NUM_ITLB_SETS (1 << ((mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_NTS) >> \
  37. SPR_IMMUCFGR_NTS_OFF))
  38. #define DTLB_OFFSET(addr) (((addr) >> PAGE_SHIFT) & (NUM_DTLB_SETS-1))
  39. #define ITLB_OFFSET(addr) (((addr) >> PAGE_SHIFT) & (NUM_ITLB_SETS-1))
  40. /*
  41. * Invalidate all TLB entries.
  42. *
  43. * This comes down to setting the 'valid' bit for all xTLBMR registers to 0.
  44. * Easiest way to accomplish this is to just zero out the xTLBMR register
  45. * completely.
  46. *
  47. */
  48. void flush_tlb_all(void)
  49. {
  50. int i;
  51. unsigned long num_tlb_sets;
  52. /* Determine number of sets for IMMU. */
  53. /* FIXME: Assumption is I & D nsets equal. */
  54. num_tlb_sets = NUM_ITLB_SETS;
  55. for (i = 0; i < num_tlb_sets; i++) {
  56. mtspr_off(SPR_DTLBMR_BASE(0), i, 0);
  57. mtspr_off(SPR_ITLBMR_BASE(0), i, 0);
  58. }
  59. }
  60. #define have_dtlbeir (mfspr(SPR_DMMUCFGR) & SPR_DMMUCFGR_TEIRI)
  61. #define have_itlbeir (mfspr(SPR_IMMUCFGR) & SPR_IMMUCFGR_TEIRI)
  62. /*
  63. * Invalidate a single page. This is what the xTLBEIR register is for.
  64. *
  65. * There's no point in checking the vma for PAGE_EXEC to determine whether it's
  66. * the data or instruction TLB that should be flushed... that would take more
  67. * than the few instructions that the following compiles down to!
  68. *
  69. * The case where we don't have the xTLBEIR register really only works for
  70. * MMU's with a single way and is hard-coded that way.
  71. */
  72. #define flush_dtlb_page_eir(addr) mtspr(SPR_DTLBEIR, addr)
  73. #define flush_dtlb_page_no_eir(addr) \
  74. mtspr_off(SPR_DTLBMR_BASE(0), DTLB_OFFSET(addr), 0);
  75. #define flush_itlb_page_eir(addr) mtspr(SPR_ITLBEIR, addr)
  76. #define flush_itlb_page_no_eir(addr) \
  77. mtspr_off(SPR_ITLBMR_BASE(0), ITLB_OFFSET(addr), 0);
  78. void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
  79. {
  80. if (have_dtlbeir)
  81. flush_dtlb_page_eir(addr);
  82. else
  83. flush_dtlb_page_no_eir(addr);
  84. if (have_itlbeir)
  85. flush_itlb_page_eir(addr);
  86. else
  87. flush_itlb_page_no_eir(addr);
  88. }
  89. void flush_tlb_range(struct vm_area_struct *vma,
  90. unsigned long start, unsigned long end)
  91. {
  92. int addr;
  93. bool dtlbeir;
  94. bool itlbeir;
  95. dtlbeir = have_dtlbeir;
  96. itlbeir = have_itlbeir;
  97. for (addr = start; addr < end; addr += PAGE_SIZE) {
  98. if (dtlbeir)
  99. flush_dtlb_page_eir(addr);
  100. else
  101. flush_dtlb_page_no_eir(addr);
  102. if (itlbeir)
  103. flush_itlb_page_eir(addr);
  104. else
  105. flush_itlb_page_no_eir(addr);
  106. }
  107. }
  108. /*
  109. * Invalidate the selected mm context only.
  110. *
  111. * FIXME: Due to some bug here, we're flushing everything for now.
  112. * This should be changed to loop over over mm and call flush_tlb_range.
  113. */
  114. void flush_tlb_mm(struct mm_struct *mm)
  115. {
  116. /* Was seeing bugs with the mm struct passed to us. Scrapped most of
  117. this function. */
  118. /* Several architctures do this */
  119. flush_tlb_all();
  120. }
  121. /* called in schedule() just before actually doing the switch_to */
  122. void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  123. struct task_struct *next_tsk)
  124. {
  125. /* remember the pgd for the fault handlers
  126. * this is similar to the pgd register in some other CPU's.
  127. * we need our own copy of it because current and active_mm
  128. * might be invalid at points where we still need to derefer
  129. * the pgd.
  130. */
  131. current_pgd = next->pgd;
  132. /* We don't have context support implemented, so flush all
  133. * entries belonging to previous map
  134. */
  135. if (prev != next)
  136. flush_tlb_mm(prev);
  137. }
  138. /*
  139. * Initialize the context related info for a new mm_struct
  140. * instance.
  141. */
  142. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  143. {
  144. mm->context = NO_CONTEXT;
  145. return 0;
  146. }
  147. /* called by __exit_mm to destroy the used MMU context if any before
  148. * destroying the mm itself. this is only called when the last user of the mm
  149. * drops it.
  150. */
  151. void destroy_context(struct mm_struct *mm)
  152. {
  153. flush_tlb_mm(mm);
  154. }
  155. /* called once during VM initialization, from init.c */
  156. void __init tlb_init(void)
  157. {
  158. /* Do nothing... */
  159. /* invalidate the entire TLB */
  160. /* flush_tlb_all(); */
  161. }