iSeries_htab.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * iSeries hashtable management.
  3. * Derived from pSeries_htab.c
  4. *
  5. * SMP scalability work:
  6. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <asm/machdep.h>
  14. #include <asm/pgtable.h>
  15. #include <asm/mmu.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/iSeries/HvCallHpt.h>
  18. #include <asm/abs_addr.h>
  19. #include <linux/spinlock.h>
  20. static spinlock_t iSeries_hlocks[64] __cacheline_aligned_in_smp = { [0 ... 63] = SPIN_LOCK_UNLOCKED};
  21. /*
  22. * Very primitive algorithm for picking up a lock
  23. */
  24. static inline void iSeries_hlock(unsigned long slot)
  25. {
  26. if (slot & 0x8)
  27. slot = ~slot;
  28. spin_lock(&iSeries_hlocks[(slot >> 4) & 0x3f]);
  29. }
  30. static inline void iSeries_hunlock(unsigned long slot)
  31. {
  32. if (slot & 0x8)
  33. slot = ~slot;
  34. spin_unlock(&iSeries_hlocks[(slot >> 4) & 0x3f]);
  35. }
  36. static long iSeries_hpte_insert(unsigned long hpte_group, unsigned long va,
  37. unsigned long prpn, unsigned long vflags,
  38. unsigned long rflags)
  39. {
  40. unsigned long arpn;
  41. long slot;
  42. hpte_t lhpte;
  43. int secondary = 0;
  44. /*
  45. * The hypervisor tries both primary and secondary.
  46. * If we are being called to insert in the secondary,
  47. * it means we have already tried both primary and secondary,
  48. * so we return failure immediately.
  49. */
  50. if (vflags & HPTE_V_SECONDARY)
  51. return -1;
  52. iSeries_hlock(hpte_group);
  53. slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
  54. BUG_ON(lhpte.v & HPTE_V_VALID);
  55. if (slot == -1) { /* No available entry found in either group */
  56. iSeries_hunlock(hpte_group);
  57. return -1;
  58. }
  59. if (slot < 0) { /* MSB set means secondary group */
  60. vflags |= HPTE_V_VALID;
  61. secondary = 1;
  62. slot &= 0x7fffffffffffffff;
  63. }
  64. arpn = phys_to_abs(prpn << PAGE_SHIFT) >> PAGE_SHIFT;
  65. lhpte.v = (va >> 23) << HPTE_V_AVPN_SHIFT | vflags | HPTE_V_VALID;
  66. lhpte.r = (arpn << HPTE_R_RPN_SHIFT) | rflags;
  67. /* Now fill in the actual HPTE */
  68. HvCallHpt_addValidate(slot, secondary, &lhpte);
  69. iSeries_hunlock(hpte_group);
  70. return (secondary << 3) | (slot & 7);
  71. }
  72. static unsigned long iSeries_hpte_getword0(unsigned long slot)
  73. {
  74. hpte_t hpte;
  75. HvCallHpt_get(&hpte, slot);
  76. return hpte.v;
  77. }
  78. static long iSeries_hpte_remove(unsigned long hpte_group)
  79. {
  80. unsigned long slot_offset;
  81. int i;
  82. unsigned long hpte_v;
  83. /* Pick a random slot to start at */
  84. slot_offset = mftb() & 0x7;
  85. iSeries_hlock(hpte_group);
  86. for (i = 0; i < HPTES_PER_GROUP; i++) {
  87. hpte_v = iSeries_hpte_getword0(hpte_group + slot_offset);
  88. if (! (hpte_v & HPTE_V_BOLTED)) {
  89. HvCallHpt_invalidateSetSwBitsGet(hpte_group +
  90. slot_offset, 0, 0);
  91. iSeries_hunlock(hpte_group);
  92. return i;
  93. }
  94. slot_offset++;
  95. slot_offset &= 0x7;
  96. }
  97. iSeries_hunlock(hpte_group);
  98. return -1;
  99. }
  100. /*
  101. * The HyperVisor expects the "flags" argument in this form:
  102. * bits 0..59 : reserved
  103. * bit 60 : N
  104. * bits 61..63 : PP2,PP1,PP0
  105. */
  106. static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp,
  107. unsigned long va, int large, int local)
  108. {
  109. hpte_t hpte;
  110. unsigned long avpn = va >> 23;
  111. iSeries_hlock(slot);
  112. HvCallHpt_get(&hpte, slot);
  113. if ((HPTE_V_AVPN_VAL(hpte.v) == avpn) && (hpte.v & HPTE_V_VALID)) {
  114. /*
  115. * Hypervisor expects bits as NPPP, which is
  116. * different from how they are mapped in our PP.
  117. */
  118. HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1));
  119. iSeries_hunlock(slot);
  120. return 0;
  121. }
  122. iSeries_hunlock(slot);
  123. return -1;
  124. }
  125. /*
  126. * Functions used to find the PTE for a particular virtual address.
  127. * Only used during boot when bolting pages.
  128. *
  129. * Input : vpn : virtual page number
  130. * Output: PTE index within the page table of the entry
  131. * -1 on failure
  132. */
  133. static long iSeries_hpte_find(unsigned long vpn)
  134. {
  135. hpte_t hpte;
  136. long slot;
  137. /*
  138. * The HvCallHpt_findValid interface is as follows:
  139. * 0xffffffffffffffff : No entry found.
  140. * 0x00000000xxxxxxxx : Entry found in primary group, slot x
  141. * 0x80000000xxxxxxxx : Entry found in secondary group, slot x
  142. */
  143. slot = HvCallHpt_findValid(&hpte, vpn);
  144. if (hpte.v & HPTE_V_VALID) {
  145. if (slot < 0) {
  146. slot &= 0x7fffffffffffffff;
  147. slot = -slot;
  148. }
  149. } else
  150. slot = -1;
  151. return slot;
  152. }
  153. /*
  154. * Update the page protection bits. Intended to be used to create
  155. * guard pages for kernel data structures on pages which are bolted
  156. * in the HPT. Assumes pages being operated on will not be stolen.
  157. * Does not work on large pages.
  158. *
  159. * No need to lock here because we should be the only user.
  160. */
  161. static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
  162. {
  163. unsigned long vsid,va,vpn;
  164. long slot;
  165. vsid = get_kernel_vsid(ea);
  166. va = (vsid << 28) | (ea & 0x0fffffff);
  167. vpn = va >> PAGE_SHIFT;
  168. slot = iSeries_hpte_find(vpn);
  169. if (slot == -1)
  170. panic("updateboltedpp: Could not find page to bolt\n");
  171. HvCallHpt_setPp(slot, newpp);
  172. }
  173. static void iSeries_hpte_invalidate(unsigned long slot, unsigned long va,
  174. int large, int local)
  175. {
  176. unsigned long hpte_v;
  177. unsigned long avpn = va >> 23;
  178. unsigned long flags;
  179. local_irq_save(flags);
  180. iSeries_hlock(slot);
  181. hpte_v = iSeries_hpte_getword0(slot);
  182. if ((HPTE_V_AVPN_VAL(hpte_v) == avpn) && (hpte_v & HPTE_V_VALID))
  183. HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0);
  184. iSeries_hunlock(slot);
  185. local_irq_restore(flags);
  186. }
  187. void hpte_init_iSeries(void)
  188. {
  189. ppc_md.hpte_invalidate = iSeries_hpte_invalidate;
  190. ppc_md.hpte_updatepp = iSeries_hpte_updatepp;
  191. ppc_md.hpte_updateboltedpp = iSeries_hpte_updateboltedpp;
  192. ppc_md.hpte_insert = iSeries_hpte_insert;
  193. ppc_md.hpte_remove = iSeries_hpte_remove;
  194. htab_finish_init();
  195. }