iSeries_htab.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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, int secondary,
  38. unsigned long hpteflags, int bolted, int large)
  39. {
  40. long slot;
  41. HPTE lhpte;
  42. /*
  43. * The hypervisor tries both primary and secondary.
  44. * If we are being called to insert in the secondary,
  45. * it means we have already tried both primary and secondary,
  46. * so we return failure immediately.
  47. */
  48. if (secondary)
  49. return -1;
  50. iSeries_hlock(hpte_group);
  51. slot = HvCallHpt_findValid(&lhpte, va >> PAGE_SHIFT);
  52. BUG_ON(lhpte.dw0.dw0.v);
  53. if (slot == -1) { /* No available entry found in either group */
  54. iSeries_hunlock(hpte_group);
  55. return -1;
  56. }
  57. if (slot < 0) { /* MSB set means secondary group */
  58. secondary = 1;
  59. slot &= 0x7fffffffffffffff;
  60. }
  61. lhpte.dw1.dword1 = 0;
  62. lhpte.dw1.dw1.rpn = physRpn_to_absRpn(prpn);
  63. lhpte.dw1.flags.flags = hpteflags;
  64. lhpte.dw0.dword0 = 0;
  65. lhpte.dw0.dw0.avpn = va >> 23;
  66. lhpte.dw0.dw0.h = secondary;
  67. lhpte.dw0.dw0.bolted = bolted;
  68. lhpte.dw0.dw0.v = 1;
  69. /* Now fill in the actual HPTE */
  70. HvCallHpt_addValidate(slot, secondary, &lhpte);
  71. iSeries_hunlock(hpte_group);
  72. return (secondary << 3) | (slot & 7);
  73. }
  74. static unsigned long iSeries_hpte_getword0(unsigned long slot)
  75. {
  76. unsigned long dword0;
  77. HPTE hpte;
  78. HvCallHpt_get(&hpte, slot);
  79. dword0 = hpte.dw0.dword0;
  80. return dword0;
  81. }
  82. static long iSeries_hpte_remove(unsigned long hpte_group)
  83. {
  84. unsigned long slot_offset;
  85. int i;
  86. HPTE lhpte;
  87. /* Pick a random slot to start at */
  88. slot_offset = mftb() & 0x7;
  89. iSeries_hlock(hpte_group);
  90. for (i = 0; i < HPTES_PER_GROUP; i++) {
  91. lhpte.dw0.dword0 =
  92. iSeries_hpte_getword0(hpte_group + slot_offset);
  93. if (!lhpte.dw0.dw0.bolted) {
  94. HvCallHpt_invalidateSetSwBitsGet(hpte_group +
  95. slot_offset, 0, 0);
  96. iSeries_hunlock(hpte_group);
  97. return i;
  98. }
  99. slot_offset++;
  100. slot_offset &= 0x7;
  101. }
  102. iSeries_hunlock(hpte_group);
  103. return -1;
  104. }
  105. /*
  106. * The HyperVisor expects the "flags" argument in this form:
  107. * bits 0..59 : reserved
  108. * bit 60 : N
  109. * bits 61..63 : PP2,PP1,PP0
  110. */
  111. static long iSeries_hpte_updatepp(unsigned long slot, unsigned long newpp,
  112. unsigned long va, int large, int local)
  113. {
  114. HPTE hpte;
  115. unsigned long avpn = va >> 23;
  116. iSeries_hlock(slot);
  117. HvCallHpt_get(&hpte, slot);
  118. if ((hpte.dw0.dw0.avpn == avpn) && (hpte.dw0.dw0.v)) {
  119. /*
  120. * Hypervisor expects bits as NPPP, which is
  121. * different from how they are mapped in our PP.
  122. */
  123. HvCallHpt_setPp(slot, (newpp & 0x3) | ((newpp & 0x4) << 1));
  124. iSeries_hunlock(slot);
  125. return 0;
  126. }
  127. iSeries_hunlock(slot);
  128. return -1;
  129. }
  130. /*
  131. * Functions used to find the PTE for a particular virtual address.
  132. * Only used during boot when bolting pages.
  133. *
  134. * Input : vpn : virtual page number
  135. * Output: PTE index within the page table of the entry
  136. * -1 on failure
  137. */
  138. static long iSeries_hpte_find(unsigned long vpn)
  139. {
  140. HPTE hpte;
  141. long slot;
  142. /*
  143. * The HvCallHpt_findValid interface is as follows:
  144. * 0xffffffffffffffff : No entry found.
  145. * 0x00000000xxxxxxxx : Entry found in primary group, slot x
  146. * 0x80000000xxxxxxxx : Entry found in secondary group, slot x
  147. */
  148. slot = HvCallHpt_findValid(&hpte, vpn);
  149. if (hpte.dw0.dw0.v) {
  150. if (slot < 0) {
  151. slot &= 0x7fffffffffffffff;
  152. slot = -slot;
  153. }
  154. } else
  155. slot = -1;
  156. return slot;
  157. }
  158. /*
  159. * Update the page protection bits. Intended to be used to create
  160. * guard pages for kernel data structures on pages which are bolted
  161. * in the HPT. Assumes pages being operated on will not be stolen.
  162. * Does not work on large pages.
  163. *
  164. * No need to lock here because we should be the only user.
  165. */
  166. static void iSeries_hpte_updateboltedpp(unsigned long newpp, unsigned long ea)
  167. {
  168. unsigned long vsid,va,vpn;
  169. long slot;
  170. vsid = get_kernel_vsid(ea);
  171. va = (vsid << 28) | (ea & 0x0fffffff);
  172. vpn = va >> PAGE_SHIFT;
  173. slot = iSeries_hpte_find(vpn);
  174. if (slot == -1)
  175. panic("updateboltedpp: Could not find page to bolt\n");
  176. HvCallHpt_setPp(slot, newpp);
  177. }
  178. static void iSeries_hpte_invalidate(unsigned long slot, unsigned long va,
  179. int large, int local)
  180. {
  181. HPTE lhpte;
  182. unsigned long avpn = va >> 23;
  183. unsigned long flags;
  184. local_irq_save(flags);
  185. iSeries_hlock(slot);
  186. lhpte.dw0.dword0 = iSeries_hpte_getword0(slot);
  187. if ((lhpte.dw0.dw0.avpn == avpn) && lhpte.dw0.dw0.v)
  188. HvCallHpt_invalidateSetSwBitsGet(slot, 0, 0);
  189. iSeries_hunlock(slot);
  190. local_irq_restore(flags);
  191. }
  192. void hpte_init_iSeries(void)
  193. {
  194. ppc_md.hpte_invalidate = iSeries_hpte_invalidate;
  195. ppc_md.hpte_updatepp = iSeries_hpte_updatepp;
  196. ppc_md.hpte_updateboltedpp = iSeries_hpte_updateboltedpp;
  197. ppc_md.hpte_insert = iSeries_hpte_insert;
  198. ppc_md.hpte_remove = iSeries_hpte_remove;
  199. htab_finish_init();
  200. }