tlb-sh5.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * arch/sh/mm/tlb-sh5.c
  3. *
  4. * Copyright (C) 2003 Paul Mundt <lethal@linux-sh.org>
  5. * Copyright (C) 2003 Richard Curnow <richard.curnow@superh.com>
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/init.h>
  13. #include <asm/page.h>
  14. #include <asm/tlb.h>
  15. #include <asm/mmu_context.h>
  16. /**
  17. * sh64_tlb_init - Perform initial setup for the DTLB and ITLB.
  18. */
  19. int sh64_tlb_init(void)
  20. {
  21. /* Assign some sane DTLB defaults */
  22. cpu_data->dtlb.entries = 64;
  23. cpu_data->dtlb.step = 0x10;
  24. cpu_data->dtlb.first = DTLB_FIXED | cpu_data->dtlb.step;
  25. cpu_data->dtlb.next = cpu_data->dtlb.first;
  26. cpu_data->dtlb.last = DTLB_FIXED |
  27. ((cpu_data->dtlb.entries - 1) *
  28. cpu_data->dtlb.step);
  29. /* And again for the ITLB */
  30. cpu_data->itlb.entries = 64;
  31. cpu_data->itlb.step = 0x10;
  32. cpu_data->itlb.first = ITLB_FIXED | cpu_data->itlb.step;
  33. cpu_data->itlb.next = cpu_data->itlb.first;
  34. cpu_data->itlb.last = ITLB_FIXED |
  35. ((cpu_data->itlb.entries - 1) *
  36. cpu_data->itlb.step);
  37. return 0;
  38. }
  39. /**
  40. * sh64_next_free_dtlb_entry - Find the next available DTLB entry
  41. */
  42. unsigned long long sh64_next_free_dtlb_entry(void)
  43. {
  44. return cpu_data->dtlb.next;
  45. }
  46. /**
  47. * sh64_get_wired_dtlb_entry - Allocate a wired (locked-in) entry in the DTLB
  48. */
  49. unsigned long long sh64_get_wired_dtlb_entry(void)
  50. {
  51. unsigned long long entry = sh64_next_free_dtlb_entry();
  52. cpu_data->dtlb.first += cpu_data->dtlb.step;
  53. cpu_data->dtlb.next += cpu_data->dtlb.step;
  54. return entry;
  55. }
  56. /**
  57. * sh64_put_wired_dtlb_entry - Free a wired (locked-in) entry in the DTLB.
  58. *
  59. * @entry: Address of TLB slot.
  60. *
  61. * Works like a stack, last one to allocate must be first one to free.
  62. */
  63. int sh64_put_wired_dtlb_entry(unsigned long long entry)
  64. {
  65. __flush_tlb_slot(entry);
  66. /*
  67. * We don't do any particularly useful tracking of wired entries,
  68. * so this approach works like a stack .. last one to be allocated
  69. * has to be the first one to be freed.
  70. *
  71. * We could potentially load wired entries into a list and work on
  72. * rebalancing the list periodically (which also entails moving the
  73. * contents of a TLB entry) .. though I have a feeling that this is
  74. * more trouble than it's worth.
  75. */
  76. /*
  77. * Entry must be valid .. we don't want any ITLB addresses!
  78. */
  79. if (entry <= DTLB_FIXED)
  80. return -EINVAL;
  81. /*
  82. * Next, check if we're within range to be freed. (ie, must be the
  83. * entry beneath the first 'free' entry!
  84. */
  85. if (entry < (cpu_data->dtlb.first - cpu_data->dtlb.step))
  86. return -EINVAL;
  87. /* If we are, then bring this entry back into the list */
  88. cpu_data->dtlb.first -= cpu_data->dtlb.step;
  89. cpu_data->dtlb.next = entry;
  90. return 0;
  91. }
  92. /**
  93. * sh64_setup_tlb_slot - Load up a translation in a wired slot.
  94. *
  95. * @config_addr: Address of TLB slot.
  96. * @eaddr: Virtual address.
  97. * @asid: Address Space Identifier.
  98. * @paddr: Physical address.
  99. *
  100. * Load up a virtual<->physical translation for @eaddr<->@paddr in the
  101. * pre-allocated TLB slot @config_addr (see sh64_get_wired_dtlb_entry).
  102. */
  103. void sh64_setup_tlb_slot(unsigned long long config_addr, unsigned long eaddr,
  104. unsigned long asid, unsigned long paddr)
  105. {
  106. unsigned long long pteh, ptel;
  107. pteh = neff_sign_extend(eaddr);
  108. pteh &= PAGE_MASK;
  109. pteh |= (asid << PTEH_ASID_SHIFT) | PTEH_VALID;
  110. ptel = neff_sign_extend(paddr);
  111. ptel &= PAGE_MASK;
  112. ptel |= (_PAGE_CACHABLE | _PAGE_READ | _PAGE_WRITE);
  113. asm volatile("putcfg %0, 1, %1\n\t"
  114. "putcfg %0, 0, %2\n"
  115. : : "r" (config_addr), "r" (ptel), "r" (pteh));
  116. }
  117. /**
  118. * sh64_teardown_tlb_slot - Teardown a translation.
  119. *
  120. * @config_addr: Address of TLB slot.
  121. *
  122. * Teardown any existing mapping in the TLB slot @config_addr.
  123. */
  124. void sh64_teardown_tlb_slot(unsigned long long config_addr)
  125. __attribute__ ((alias("__flush_tlb_slot")));
  126. static int dtlb_entry;
  127. static unsigned long long dtlb_entries[64];
  128. void tlb_wire_entry(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  129. {
  130. unsigned long long entry;
  131. unsigned long paddr, flags;
  132. BUG_ON(dtlb_entry == ARRAY_SIZE(dtlb_entries));
  133. local_irq_save(flags);
  134. entry = sh64_get_wired_dtlb_entry();
  135. dtlb_entries[dtlb_entry++] = entry;
  136. paddr = pte_val(pte) & _PAGE_FLAGS_HARDWARE_MASK;
  137. paddr &= ~PAGE_MASK;
  138. sh64_setup_tlb_slot(entry, addr, get_asid(), paddr);
  139. local_irq_restore(flags);
  140. }
  141. void tlb_unwire_entry(void)
  142. {
  143. unsigned long long entry;
  144. unsigned long flags;
  145. BUG_ON(!dtlb_entry);
  146. local_irq_save(flags);
  147. entry = dtlb_entries[dtlb_entry--];
  148. sh64_teardown_tlb_slot(entry);
  149. sh64_put_wired_dtlb_entry(entry);
  150. local_irq_restore(flags);
  151. }
  152. void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  153. {
  154. unsigned long long ptel;
  155. unsigned long long pteh=0;
  156. struct tlb_info *tlbp;
  157. unsigned long long next;
  158. unsigned int fault_code = get_thread_fault_code();
  159. /* Get PTEL first */
  160. ptel = pte.pte_low;
  161. /*
  162. * Set PTEH register
  163. */
  164. pteh = neff_sign_extend(address & MMU_VPN_MASK);
  165. /* Set the ASID. */
  166. pteh |= get_asid() << PTEH_ASID_SHIFT;
  167. pteh |= PTEH_VALID;
  168. /* Set PTEL register, set_pte has performed the sign extension */
  169. ptel &= _PAGE_FLAGS_HARDWARE_MASK; /* drop software flags */
  170. if (fault_code & FAULT_CODE_ITLB)
  171. tlbp = &cpu_data->itlb;
  172. else
  173. tlbp = &cpu_data->dtlb;
  174. next = tlbp->next;
  175. __flush_tlb_slot(next);
  176. asm volatile ("putcfg %0,1,%2\n\n\t"
  177. "putcfg %0,0,%1\n"
  178. : : "r" (next), "r" (pteh), "r" (ptel) );
  179. next += TLB_STEP;
  180. if (next > tlbp->last)
  181. next = tlbp->first;
  182. tlbp->next = next;
  183. }